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.
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.
04-24-2015 02:07 PM
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).
Solved! Go to Solution.
04-24-2015 02:54 PM
This can be done with auto-indexing with conditional tunnels. The same approach could easily be applied to a 2D array, as well:
04-24-2015 02:58 PM
Perfect!
08-08-2019 02:55 PM
Is there a way to do this without a loop, i.e. A[0,1,0,1,1] = [A(1), A(3), A(4)] ?
08-08-2019 03:20 PM
@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. 😄
08-08-2019 03:31 PM
Just wondering if there is a more efficient way to do it.
08-08-2019 03:37 PM
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.
08-08-2019 04:10 PM - edited 08-08-2019 05:04 PM
@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?
08-09-2019 09:32 AM
" 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 🙂
08-22-2019 03:10 PM
@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.