08-12-2009 02:49 PM
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
Solved! Go to Solution.
08-12-2009 02:55 PM
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 ]. |
08-12-2009 03:02 PM
08-12-2009 03:03 PM
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.
08-12-2009 03:08 PM
08-12-2009 03:13 PM
to Cory K:
you should initialize the shift register with an empty array, otherwise the output array gets longer and longer with every run.
08-12-2009 03:14 PM
08-12-2009 03:24 PM
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.
08-12-2009 03:42 PM
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
08-12-2009 03:49 PM - edited 08-12-2009 03:50 PM
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