LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Fortran code to mathscript syntax

Solved!
Go to solution

So I have a 2D data set (at the moment it is a set of random numbers) that I have obtained from histogram binning of 100,000 data points into 1000 bins.  One column (column 0) are the binned intensity (I) values I am after and the other column (column 1) is the number of these binned intensity values that have been binned (ie # of observations, N)

 

Now what I need to do now is to go through the entire array (ie the 1000 bins) and find the maximum product  of N*I, where these values of N and I are now Nm and Im respectively (so the maximum product is Nm*Im).

 

What I have done is to separate the two columns into separate arrays, one for intensity and one for number of observations.  Now the best way that I could think to find the maximum product is use the Mathscript Node.  I have a Fortran code that has previously done this, this is shown here;

 

 nximax=0

      do 140 ints=1,1000

        if(num(ints)*ints.gt.nximax)then

          nummost=num(ints)

          intmost=ints

          nximax=nummost*intmost

        end if

140   continue

 

 

(note that in my node code I have changed - nximax to NmIm, nummost to Nm, intmost to Im, num(inst) to N and inst to I)

 

 But I find that when I run the code as I have it, it doesn't work.  I am not sure that my prepration of the data prior to going  into the node is correct (I tried putting it in a For Loop but it didn't like that (it said the array was wired wrong, and probably most importantly I don't think I have made the conversion from the Fortran code (as detailed above) to an appropriate mathscript syntax. 

 

So the help I am after here is a double check on my wiring prior to the data entering the mathscript node (especially given that I want to get the product of the Intensity and observation number for each bin) and how to correctly convert the fortran code to mathscript code so that it does the same thing.

 

Any help would be appreciated.

 

I am using LV 8.6.1

 

I have attached thevi of interest: PE profile #1.vi

and some random data I have put together.

 

Thanks,

 

Scott

Download All
0 Kudos
Message 1 of 8
(3,574 Views)

Labview has a max/min function; its in the math functions group.

0 Kudos
Message 2 of 8
(3,556 Views)
Solution
Accepted by topic author scottum
Hello Scott,

Your wiring code looks good.  In general, when working with arrays, you should avoid processing elements one at a time in loops in MathScript.  If you want to compute the maximum product of N * I, you can do that with just a few simple instructions.  One thing to note is that you want to perform a scalar multiplication of two vectors (N and I) -- note the dot before the times.  If you use the regular multiplication, it will try to do a matrix multiplication and return an error.  If you browse the LabVIEW help for the max function, you will see it can return two outputs: the maximum value and the index in the array where it occurred.  You can use these values to get the information you want:

[NmIm index] = max(N .* I);
Nm = N(index);
Im = I(index);


Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 3 of 8
(3,543 Views)

For programmers coming from other languages into LabVIEW, one of the hardest things to master is ALL THE CRAP YOU DON'T HAVE TO DO ANYMORE.

 

It's not clear from your description whether you need the PAIR that produces the largest product, or the INDIVIDUALS that produce the largest product. 

So here's both ways.

 

In the upper part, you multiply A x B, and pick out the max of the result.

In the lower part you pick out the max of the two sources, and multiply them.

 

 

 

Message Edited by CoastalMaineBird on 03-31-2009 04:39 PM
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 4 of 8
(3,531 Views)

@Mr6dof - I was aware of Labview having a Max/min array function but not one in the math functions group (and from a quick check I couldn't see it).

 

@GrantM - thanks that seems to work perfectly. I appreciate it. I will try to let you know how it goes when I eventually get some real (ie experimental data) going through.

 

@CoastalMaineBird - My issue is that I don't have any programming experience other than what I am getting at the moment using Labview. But I do take your point about not always having to do stuff the same way. Hence my question of how to translate the fortran code to mathscript code (ie I am not exactly familiar with either). Sorry I wasn't clear about what exactly I was after here, it was the the pair the produces the larget product. And from that I need the value of the product and also the the N and I values that gave rise to that value (ie Nm and Im). I had actually considered your top approach, but I wasn't enitrley sure of how to then take the index of the max product and extract from that the N and I values at that index value (is there actually an easy way to do that). Hence me then going onto the mathscript node stuff where I thought it might be possible to find a way to mimic the fortan code there. But thanks for taking the time to give me some feedback and it is always good to come across different approaches, especially in programming. You never know when it might come in useful

 

Somewhat off topic here - just thought I would ask, how do you edit your posts? --> just realised you can edit a post yuo just made, but not an older post (I had always been trying it with older posts)

 

Thanks again,

 

Scott

Message Edited by scottum on 04-01-2009 09:30 AM
0 Kudos
Message 5 of 8
(3,510 Views)

Cool, just be aware that there are some VERY powerful tools under the LabVIEW hood, and don't limit yourself to the same ways of thinking as are in other languages. The fact that an array can be multiplied by an array is just such an idea.

 

 how do you edit your posts? --> just realised you can edit a post yuo just made, but not an older post (I had always been trying it with older posts)

 

 

Yeah, you can only edit your own post, and only if it's the LAST post.  When somebody else comes along, your post gets written in ink.

 

You display a picture in-line by first submitting the pic as an attachment. That gives you the address on NI servers where it resides.  Then you edit the post to include an IMG SRC tag, with the address you just discovered.

 

Or you edit it to fix the stoopid typos when yuo fat-fingered the keebored. ;-> 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 6 of 8
(3,494 Views)

CoastalMaineBird wrote:

 how do you edit your posts? --> just realised you can edit a post yuo just made, but not an older post (I had always been trying it with older posts)

 

 

Yeah, you can only edit your own post, and only if it's the LAST post.  When somebody else comes along, your post gets written in ink.

 


No.  You can only edit a post within 10 minutes of when you first submitted it.  If you wait until after 10 minutes, the Edit option disappears from the option menu.  If you start editing within the 10 minute window, but it takes you too long and you are outside the 10 minutes when you submit your edited post, you will get a timeout error message.

 

You are able to edit your posts even if someone has already submitted a message immediately after yours.  You can see examples of this occurring in the middle of the day when the forums are busy and several people are all replying to the same thread at once.

Message 7 of 8
(3,488 Views)

You can only edit a post within 10 minutes of when you first submitted it.

 

I stand corrected.  I guess when I noticed it, the 10 minute period, and the next post happened about the same time, and I guessed wrongly. 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 8 of 8
(3,469 Views)