LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how do I search and return multiple elements of array quickly (like Matlab find)?

Solved!
Go to solution

Hi, I am a fairly experienced Labview programmer, and I have always wondered if there is a way to quickly search arrays for multiple elements that meet a selection criteria, equivalent to the Matlab find command.  While my inability to do this in Labview has always bugged me, I now have an application that requires this ability and I need to find a solution. 

 

The fastest way that I have found to do this in labview is to perform comparisons on vectors of data, and then use the resulting Boolean vector to parse the array using a for loop and shift registers.  I have attached a vi snippet of a benchmarking vi for one of the simple searches I need to do.  On my laptop this search takes about 600ms.  In comparison, the equivalent in Matlab:

 

newArray=oldArray(find(oldArray(:,4)./oldArray(:,2)>1.5),:);

 

executes in about 1.2ms.  So, the way that I am doing this in Labview is only 400 times slower 🙂

 

Needless to say, I need to do these types of searches many many times, and those 599 extra ms start to add up pretty quickly!

 

Thanks for your help,

Aaron    

0 Kudos
Message 1 of 3
(4,218 Views)
Solution
Accepted by topic author AaronBlake

Hi Aaron,

 

the slowest part is probably the "build array" node in the loop.

 

Try this:

Define an array of same size as input data (or simply use a copy of it) and wire that to the shift register. Keep a counter of found rows in the loop. Use IndexArray and ReplaceArraySubset in the loop to move the found rows to beginning of array (overwrite "bad" rows). After the loop simply ReshapeArray to the number of found rows...

 

Sorry, don't have LV(2009) at hand to edit your snippet.

Message Edited by GerdW on 11-20-2009 08:27 PM
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 3
(4,206 Views)

GerdW, thanks for your fast reply, you are right on.  I pre-alocated the returns array and the average execution time dropped to order 1ms.  This is a good reminder for me to pay attention to fundamentals!

 

 

Thanks again,

Aaron

 

Attached is the updated vi snippet.

 

 

0 Kudos
Message 3 of 3
(4,192 Views)