LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract elements with a fixed interval from a 1D array

Solved!
Go to solution

I would like to extract elements from a 1D array with a fixed spacing. For example, I want to extract the elements from this array

[ 1 2 3 4 5 6 7 8 9 10 ] with the interval of 2 and starting at 1, the results should be [ 1 3 5 7 9 ].

 

I know I can use "Decimate 1D array" to complete this task. However, my interval is determined by something else inside my program. I don't know the interval in advance. I cannot go to the block diagram every time just to specify the number of output terminals manually. Is there a good way to do this task?

 

Thanks a lot,

herb 

0 Kudos
Message 1 of 20
(5,147 Views)

Sorry, just found that my example might be a little confusing... here goes again:

 

I would like to extract elements from a 1D array with a fixed spacing. For example, if I want to extract the elements from this array

[ 2 8 45 34 8 90 20 39 50 78 ] with the spacing of 2 starting at the first element, the results should be [ 2 45 8 20 50 ].

0 Kudos
Message 2 of 20
(5,144 Views)
use the reshape array function and make a 2D-Array, then pick a column
0 Kudos
Message 3 of 20
(5,140 Views)

What would you like to use to determine the spacing?

You can make it variable using a control, or some other value from your block diagram.

 

See the attached example.

Cory K
0 Kudos
Message 4 of 20
(5,141 Views)
A second solution
0 Kudos
Message 5 of 20
(5,146 Views)

to Cory K:

 

you should initialize the shift register with an empty array, otherwise the output array gets longer and longer with every run.

0 Kudos
Message 6 of 20
(5,142 Views)
Oops, wasn't paying attention. Thanks for catching that.
Cory K
0 Kudos
Message 7 of 20
(5,142 Views)

My second solution is similar to Cory K solution, but avoids building the array in the loop, which is supposed to be more time and memory consuming because the array is copied every loop. The array is initialized outside the loop and in the loop the array elements are replaced.

0 Kudos
Message 8 of 20
(5,129 Views)

Jorn,

I like your solution except I wold add a rotate array function to the beginning or a column selector to the array subset function.  The OP may not always need to start from the first element Smiley Wink


"Should be" isn't "Is" -Jay
0 Kudos
Message 9 of 20
(5,113 Views)

Right. I was too lazy to point out the also other columns can be selected as long as the column number is smaller than n.

 

And I would not have had the quickest answer Smiley Wink

Message Edited by Jörn on 2009-08-12 12.08.2009 10:50 PM
0 Kudos
Message 10 of 20
(5,093 Views)