11-20-2015 02:25 PM
Hi All,
I was wondering what the best approach to select elements out of an array via a boolean array. For example if I had a numeric array of 5 elements and a corresponding boolean array like:
numArr=[2, 3, 2, 5, 6]
booArr =[0, 1, 0, 0, 1]
I would then want an output array of [3, 6]. In MatLab/Python this is very simple in that I just use vectorized code and run something like numArr(booArr) which very easily and efficiently pulls out the correct elements in an easy format.
My initial naive approach would just be to iterate through the elements of the array one by one, check to see if there is a 'True' value in the corresponding boolean array, and keep only those elements.This would require us to preallocate an output array of w/ the expected number of 'True' values and fill in that array as we iterate through the inital data array. Maybe there is a simpler approach already built into LabVIEW? It seems trying to implement this in Labview is quite lengthy compared to the one line solution used in text based languages.
NKM
11-20-2015 02:43 PM
Pre-allocation is not necessary, the for loop will take care of that.
11-20-2015 03:52 PM
The Conditional Tunnel that Gregory used was introduced in LabVIEW 2012. If you are using an older version, you can still "take advantage of the efficiency" of this approach by using a Case statement to either append the value to the output array (that you keep in a shift register, initialized to an empty array) or not.
Bob Schor
11-20-2015 07:31 PM
11-23-2015 01:52 PM
Thanks gregoryj, this is exactly what I'm looking for. I had not known about the seperate tunneling modes within a for loop