LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to compare an array to a condition and locate the elements in a fast way?

ccyang,

nobody has mentioned an alternative to the threshold detect is the threshold peak detector found in full version 8.0.  Perhaps i am timing the VI wrong but i am not getting even 1 millisecond time for 10 million comparisons using this method. I am assuming the array is built otherwise so the comparison time is total time minus loop time. If i am timing the VI wrong- how embarrasing to be seen by the giants like ben, aldenbauch and kevin price.Smiley Surprised 

Kevin, That was the best posting I have read. Most of us would have just not reported on results that did not prove thier opinoin.  It inspired me to take the chance to post this amongst giants. 

Chris

Message 11 of 19
(1,778 Views)
Hi Chris,

yes, your timing was wrong 😞 Always think on dataflow!
Here's a corrected version (in LV8.2).
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 12 of 19
(1,771 Views)
After checking a little more carefully, I found a couple errors in my example.  Smiley Sad 

On the other hand, now that I've fixed them (I think...) it runs much faster.  Smiley Happy  I'll attach it here for what it's worth.

I'd still recommend one of the simpler implementations until / unless it proves slower than you can accomodate, which isn't likely unless your arrays approach MB size.

BTW, I agree that the Matlab find() function is really nifty.  Try making yourself a LabVIEW version.  For example, suppose you want to implement "B=A(find(A>thresh))".  First you'd perform "A > thresh" to produce a Boolean array.  Then you'd send your Boolean array and your A array into a function that will extract only the elements of A where the corresponding Boolean is True.  That collection of values will be your B output.

It's not quite as elegant as the Matlab command, but once you make the subvi, it's pretty easy to use.  I'll attach a simple version.  Note that there are some techniques that could make it more efficient based on "unitialized shift registers" and pre-allocated arrays.  This'll give you a functional starting point though.

Attachments are LV v7.1

-Kevin P.
ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 13 of 19
(1,762 Views)
Kevin,
I think your answer is the best. It would be less susceptible to array size just where in the array it would trigger.  I have a need for this in an analyser program that would speed up some of my for loop logics on massive arrays.
 
GerdW,
Woops! But still it only times out as the same as kevins, but i like his effeciency better.
 
Chris
0 Kudos
Message 14 of 19
(1,747 Views)

What'd likely be even better was if NI added options to functions like "Search 1D array", "Threshold 1D Array", etc. that allowed one to specify searching backwards starting from the last element.  The default unwired settings would still mean search forward from index 0 so as not to affect existing code.

The explicit call to "Reverse 1D Array" isn't a big deal in block diagram space, but it sure seems wasteful of CPU and memory to have to copy all the elements over just to use those built-in functions.  I'd guess NI's source code just traverses arrays by incrementing a pointer.  It couldn't be much work to add the option of decrementing the pointer instead, eh? 

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 15 of 19
(1,732 Views)
Whoa! Big thanks, Chris, GerdW, and Kevin. So many good helps. Sorry, GerdW, my LV is still 8.0 and I haven't got time to upgrade to 8.2. Is it possible that you post your solution in 8.0 again?
 
I agree that to reverse my array to look for the last element satisfying the condition takes lots of my RAM and CPU. It becomes vital when I need to handle huge amount of inputs in a short time to look for an answer, and repeat it for a very long time. On the other hand, I always need to create a for loop to look for index for the element satisfying the condition. Without creating such an explicit loop, I can save lot of time in Matlab. Matlab also allows me to search elements in a multi-dimension data. The PC crushed sometimes and Windows may become wierd when each data needs to be split into 1D, be reversed, and produce a for loop. The simulation using Matlab has no such a problem. The reality is that I must convert my Matlab simulation code to LV one for real machine control because the company has only LV drivers.
 
Thanks again for all of your discussions and opinions.
0 Kudos
Message 16 of 19
(1,721 Views)

It IS important to note, however, that FOR loops in LabVIEW are typically much, MUCH faster than in Matlab, at least they were last time I was checking things out a few years back.  In the Matlab environment, a m-file with a FOR loop would run in an interpreted mode whereas LabVIEW always runs in compiled mode.  It's drastically overly pessimistic to anticipate that LabVIEW FOR loops would slow down execution as much as and interpreted m-file FOR loop.  Remember, the LabVIEW examples posted in this thread handle arrays of 1 million elements and still beat the 100 msec time limit you targeted. 

Matlab's "vectorized" functions *do* give you nice notational elegance, and quite possibly some speed/efficiency gains over LabVIEW.  I've also liked the way its functions can act column-wise on a 2D array, producing a 1D row vector output.  You can accomplish the equivalent in LabVIEW, but you'd probably want the original 2D array to have its rows and columns transposed so it can perform its operations row-wise.

My own evaluation has come out like this: I've found I could do all my near-real-time data processing fast enough in LabVIEW that it didn't seem worth the bother of trying to send the data across from one environment to the other so that I could process with Matlab instead.  On a few occasions however, I've used Matlab for some post-processing long after the data was stored.  

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 17 of 19
(1,714 Views)

Here is the fixed program in 8.0 version.  You will not be able to use this program if you have labview base!!

 

 

Chris

Message 18 of 19
(1,710 Views)
Thanks Kevin, it is an interesting and surprising comment to me. I never compare the speed between Matlab and Labview in this way. It helps me a lot to solve the future problem.
 
And thanks, Chris. I got the program. However, the function Threshold Peak Detector has a little different result from what I expect. This function compare all the elements with the threshold, then locate every one which is larger than the threshold and its previous one is lower than that. But if the element is larger than threshold and its previous is, too, then this function ignores this element. I am looking for the first and last one which is larger than the threshold, but if the "last" one is in the end of the array, this function won't detect it. But this function is interesting. I am going to look into this further. Thanks a lot for your suggestions.
 
And very thank to everyone who joins this discussion.
0 Kudos
Message 19 of 19
(1,663 Views)