From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Use a Boolean Vector To Index an Array

Solved!
Go to solution

Say I have an array A = [1 2 3 4 5 6 7 8 9 10].

 

I also have a boolean array B = [1 1 0 0 0 0 0 0 0 1].

 

I want to use the boolean array B to pick out elements in A, creating a new array C = [1 2 10]. In MATLAB, I would just type C = A(B).

 

I'm scratching my head trying to figure out how to do this. One solution would be to run through a for loop, querying B(i), and deleting A(i) if B(i) = 1. But this would change the size of A, so that the second time I did this, my index would be wrong.

 

Anyone know a clean way of doing this?

 

BONUS: If I have a 2D AA, with multiple columns, it would be nice to use B to pick out multiple columns of AA (in MATLAB, this would be CC = AA(:,B).

0 Kudos
Message 1 of 12
(4,573 Views)
Solution
Accepted by topic author solobo

This can be done with auto-indexing with conditional tunnels.  The same approach could easily be applied to a 2D array, as well:

 booleanindex.png

Message 2 of 12
(4,560 Views)

Perfect!

0 Kudos
Message 3 of 12
(4,553 Views)

Is there a way to do this without a loop, i.e. A[0,1,0,1,1] = [A(1), A(3), A(4)] ?

0 Kudos
Message 4 of 12
(3,430 Views)

@jfalesi wrote:

Is there a way to do this without a loop, i.e. A[0,1,0,1,1] = [A(1), A(3), A(4)] ?


There is nothing wrong with a loop. What is your concern?

 

If you don't want to see the loop, select the loop and select "Edit...Create subVI" from the menu. 😄

0 Kudos
Message 5 of 12
(3,417 Views)

Just wondering if there is a more efficient way to do it.

 

 

0 Kudos
Message 6 of 12
(3,411 Views)

No.  Loops are the most efficient way to do something repeatedly.

 

Even if you find some way of doing it that doesn't explicitly have a loop within the LabVIEW block diagram, it's very likely that the PC is still executing a loop somewhere in the background you can't see.

Message 7 of 12
(3,408 Views)

@jfalesi wrote:

Just wondering if there is a more efficient way to do it.


First you need to define what you mean by "efficient". Typically this is in terms of computer resource usage.

 

There is no machine instruction to surgically operate on a large array, picking selected elements. The CPU has to loop no matter what. :o.

 

As has been mentioned, loops are one of the most efficient structures and should not be of concern at all. The harder problem for the compiler is the memory allocation for the new output array. It cannot re-use the existing memory because the elements and the size are different. If only very few elements are picked from a gigantic array, it might preemptively allocate a little bit too much.

 

Stop worrying! Tuning for efficiency is important once you reach the limits of the computer or your patience. Code like that executes in nanoseconds and is trivial compared to anything else happening on the PC. You don't need to stand on your toes at night to see the moon a little closer, right?

0 Kudos
Message 8 of 12
(3,400 Views)

" You don't need to stand on your toes at night to see the moon a little closer, right?" That is a good one. I will have to start using that 🙂

---------------------------------------------
Certified LabVIEW Developer (CLD)
There are two ways to tell somebody thanks: Kudos and Marked Solutions
0 Kudos
Message 9 of 12
(3,377 Views)

@RavensFan wrote:

No.  Loops are the most efficient way to do something repeatedly.

 

Even if you find some way of doing it that doesn't explicitly have a loop within the LabVIEW block diagram, it's very likely that the PC is still executing a loop somewhere in the background you can't see.


This is why I try to use primitives instead of loops - I don't want to force LabVIEW explicitly iterate if it's going to iterate behind the scenes in a more efficient manner.

 

So my question becomes "why isn't there a primitive to do this?" Most languages I've used support logical indexing natively.

0 Kudos
Message 10 of 12
(3,321 Views)