11-20-2009 01:11 PM
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
Solved! Go to Solution.
11-20-2009 01:26 PM - edited 11-20-2009 01:27 PM
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.
11-20-2009 01:47 PM
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.