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?

Hi, friends, I have a question and need help. I have a 1D array input in F32 format and need to compare value of each element to a constant A. I need to locate the index of the first and the last element which is larger than the constant A and get the index for these 2 elements. If none satisfies the condition, the program yields another constant B instead of 2 index values and read the next input array. "Search 1D array" only gives me the element equal to the constant during comparison but I need the element larger than the constant. "Threshold 1D array" searches a pair of elements of which one satisfies and another doesn't. In my case, for most of times the elements may exist in the beginning and the end of the array, then this vi gives me the wrong answer.
 
In my case, there is only about 0.1 to 0.3 seconds between 2 inputs, so I must obtain this result quickly.
 
I really appreciate for someone who can give me correct solution. Thank you.
0 Kudos
Message 1 of 19
(6,248 Views)
Compare the array with your number to get a boolean array. Now search the boolean array (using "Search 1D array") for the first and last TRUE element. 🙂
Message 2 of 19
(6,239 Views)

Don't have LV on my network PC, but here are quick thoughts:

1. Comparing floating-point numbers for equality is almost always a Bad Idea, which is another strike against "Search 1D Array."

2. "Threshold 1D Array" expects an array that's sorted in ascending order, which it sounds like you don't have.

3. Unless your incoming 1D array has some other characteristic to exploit, you may be stuck with raw code.  I'd think you'd be able to handle an array size of maybe 100 thousand samples in 0.1 seconds.

4. I'd bring the array into a while loop without auto-indexing.  I'd index 2 values out of the array on each pass through the loop.  One index will increment forward from the beginning of the array and the other decrements backward from the end.  Loop ends when both the forward incrementing index and backward decrementing index have found a value greater than your threshold, or when the indices "cross".   Of course, once your backword-moving indexer finds your latest value, future loops would bypass that 1/2 of the loop code, and likewise for the forward-moving one.

Hopefully someone else can post an example or screenshot.  I'd do it myself if I had LV handy here...

-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 3 of 19
(6,239 Views)

How is this for starters?

(Inspired by Christian's reply)

Ben

Message Edited by Ben on 10-27-2006 12:06 PM

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 4 of 19
(6,226 Views)
Whow! Big thanks to Ben, Kevin, and altenbach. I think it should solve my problem, and just wish it can run as fast as what I got in Matlab. You see, in Matlab, I use only 1 line result=find(image(:)>0); and get result(1) and result(end) for the outputs. I always got into trouble with timing for re-coding my program in LabView. Wish one day I can have as much knowledge and experience for LabView as you guys have to help others. Thanks again!
0 Kudos
Message 5 of 19
(6,213 Views)

The only other comment I'd add is that altenbach's and Ben's solutions are simpler in concept and less susceptible to a buggy implementation than mine.  Unless your arrays are really big (10's or 100's of thousands of elements or so), I'd expect their methods to meet your timing requirements.

If your arrays ARE really big and those methods are too slow (regularly or occasionally), it'd be because both of them make a really big Boolean array (or two) each time you call them.   ONLY then would I suggest you bother working out the details of my suggestion, where no new arrays need to be allocated.

-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.
0 Kudos
Message 6 of 19
(6,200 Views)
Thanks, Kevin. I will try both suggestions. In my case, timing is always a vital problem. I would appreciate very much if you have time and chance to send me an example because I am new to Labview and not sure if what I did is correct.
0 Kudos
Message 7 of 19
(6,195 Views)

I threw together a quick example that seemed to work pretty fast.  But then, just for comparison's sake, I timed it against the version Ben posted.  Except for a few cases where the earliest and latest matching elements were quite near the beginning and end of the array, his was faster, even for array sizes of 1 million!  I want to look it over more closely, but barring any further report back here, it appears that my method has NO advantages after all. Smiley Sad

(For test data, I generated gaussian noise, and used a comparison threshold that I varied between about 3-6 standard deviations.)

-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 8 of 19
(6,156 Views)

Kevin wrote;

" ... it appears that my method has NO advantages after all.  "

It takes guts to make a posting like that!

I suspect the bulk of the time in my version is spent allocating memory. I also suspect that the buffers once allocated remain allocated for use durring subsequent calls.

Since I suspect your version did not create an intermediate buffer (my booleans) the competition was between how quick could I fill in my buffer vs how fast can your while loop iterate.

Still guessing....

Since the while loop stalls pipe-lining, I suspect the repeated checks if the loop should stop are what is the bottle-kneck.

On the plus side!

If my guesses are correct, its nice to know that LV can run the simple solution so efficiently.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 9 of 19
(6,132 Views)
All of you indeed gave me a good help because I can't figure out a complicated program easily as a beginner of LabView. You see, if I have a 1D array A, and need to index all of the elements satisfying a condition, say, larger than a constant c, I can get it by Matlab code B=find(A>c). If it's the 4th, 6th, and 9th of the elements of A satisfying the condition, B is [4 6 9]. I can get the first one B(1), last one B(end), or any one of this array quickly. But by using LabView and timing is essential, I just don't have a clue how to solve the same problem in the same fast speed. I really appreciate for all of your suggestions. Thank you.
0 Kudos
Message 10 of 19
(6,112 Views)