LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

find indices of elements in 1d array

Solved!
Go to solution

Hello you all,

i am a very beginner in programming and using labview, so I apologies for any banality i might write.

I have a 1D array of numbers (1600 elements) and I would like to know the indices (of that array) of all the elements that are <1 and >-1. 

Is there any suggestion? I am not completely aware of all the potential functions I may use. 

Thank you very much for your help

Gio'

 

0 Kudos
Message 1 of 19
(7,339 Views)
Solution
Accepted by topic author Giostra

This is one way to do it..

 

You would wire your array to "Your Array" Control and the output is a 1D array with the indeces of elements in between -1 and 1

 

ArrayComp.png

CLD | CTD
0 Kudos
Message 2 of 19
(7,332 Views)

Well let's talk you through it. You'll have to do some sort of boolean logic for <1 and >-1. You'll have to do this for every element in the array. And then you'll have to output the indices of those elements.

 

You can use the In Range function to get an array of booleans corresponding with your array. This should result in an array where all elements that fall within your criteria are now a True boolean.

The next part is a little tricky. You could iterate through the array and output indices for all True values... but guess what, there's an awesome VI set that you can use called OpenG. OpenG contains a function called Search Array, which can receive an array and output an array of indices. (Find the OpenG in VI Package Manager and install)

In Range Array.png

Edit: Careful with Chris's solution because the In Range function is set to include upper and lower limits as in-range. You can change these settings using the right-click menu on that input.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 3 of 19
(7,328 Views)

Cool! It works perfectly, thank you very muchfor your help and fast reply!

Gio'

0 Kudos
Message 4 of 19
(7,311 Views)

I'm sorry, but Chris's solution is definitely Rube Goldberg code compared to how simple this issue is. The shift registered For loop with a Build Array function and performing the In Range calculation each iteration is not the way to do this. Here is how you can simplify that without OpenG:

In Range Array.png

This is 5x faster that that and doesn't involve building up unnecessary memory buffers.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 5 of 19
(7,294 Views)

@James.M wrote:

 

This is 5x faster that that and doesn't involve building up unnecessary memory buffers.


You are still creating an unecessary memory buffer for the boolean array, which could make a difference for very large arrays.

 

Here's what I would do instead. (We should benchmark if the absolute value function should be before or in the loop. If it is before it can probably reuse the existing buffer, but could benefit from SSE instriuctions. Impossible to tell without trying, especially since the compiler might do its own magic behind the scenes... 😄 )

 

 

 

(Only newer LabVIEW versions have conditional tunnels, so we don't really know if the OP can use any of this.)

 

 

 

Message 6 of 19
(7,284 Views)

@ChrisK88 wrote:

This is one way to do it..

 

You would wire your array to "Your Array" Control and the output is a 1D array with the indeces of elements in between -1 and 1

 

ArrayComp.png


That's pretty pointless for integer array, you might as well use an "equal zero" instead of "in range and coerce".  😄

It is also wrong for the original problem, because you are including the boundaries.

 

It would probably also be more efficient to preallocate the output array, replace elements as needed keeping track of the inser point, then trim at the end.

0 Kudos
Message 7 of 19
(7,274 Views)

Here's a benchmark for inside and outside the loop. The results are all very close and jump ahead and behind each other. I always thought it makes sense for the compiler to use a whole array at a time instead of point by point inside of a loop, but you're the expert, so I'll go with your solution Smiley Very Happy

Benchmark Array Indices.png

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 8 of 19
(7,270 Views)

Thanks, I haven't tried yet, but my guess was that "in range and coerce" is more expensive because several comparisons need to be made. In contrast, taking the absolute value is supposed to be dirt cheap.

 

The compiler itself has quite a few tricks up its sleave, so it is possible that all diagrams result in very similar code. (See also this page and  this presentation for a historical perspective. I am sure you've seen them, so these are links for the general reader of this thread :D))

0 Kudos
Message 9 of 19
(7,262 Views)

Holy smokes,

 

Yeah, that was a 2 second whip up before lunch, my biggest problem is always breaking things down in a for-loop to auto index, but the coerce function could do that for me. James-Morris and altenbach, touche gentlemen. 

 

Also, in my company we can not use OpenG or any non native LabVIEW primatives, crazy source control rules, so I always have to re-invent some stuff, like search array, where in LabVIEW it only outputs the first index, not an array of indices. 

CLD | CTD
0 Kudos
Message 10 of 19
(7,245 Views)