LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best approach to select elements from a numeric array via a boolean array.

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

0 Kudos
Message 1 of 5
(3,731 Views)

Pre-allocation is not necessary, the for loop will take care of that.

 

conditiaonl.png

Message 2 of 5
(3,717 Views)

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

0 Kudos
Message 3 of 5
(3,692 Views)

gregoryj a écrit :

Pre-allocation is not necessary, the for loop will take care of that.

 


Note that there is an OpenG vi from the OpenG toolkit that does that

 

using OpenG.png

 

Ben64

0 Kudos
Message 4 of 5
(3,665 Views)

Thanks gregoryj, this is exactly what I'm looking for. I had not known about the seperate tunneling modes within a for loop

0 Kudos
Message 5 of 5
(3,602 Views)