09-19-2013 02:06 PM - edited 09-19-2013 02:08 PM
HI
I want to rearrange the elements of an array in a certain order,based on their index.
For example:
Input array A = [1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 2]
Output array B should look like this: B = [1111111122222222]
The values in the array are random, so I cannot search for a "1" or "2".
(I will always have them in blocks of 4)
Thanks!
09-19-2013 02:11 PM - edited 09-19-2013 02:13 PM
It looks to me like your example is sorting them by their value, not index. ?
You might want to look at the array function palette.
Cameron
09-19-2013 02:20 PM
No I just put those values to show how I want them sorted.
So the output should have the values of index 0-3, followed by index 8-11, followed by index 4-7, then index 12-15.
09-19-2013 02:26 PM
09-19-2013 03:07 PM
Well I kind of wanted to avoid that!
Is there a more generic solution which I could apply?
09-19-2013 03:10 PM - edited 09-19-2013 03:11 PM
Hi developer,
you asked a specific question and you got a specific answer...
Now you want to have a "generic" answer? What's the generic question here?
Btw. there is a palette full of array functions. You might take a look there and read the help for functions like ArraySubset and ReplaceArrayElements...
09-19-2013 03:18 PM
@developer001 wrote:
HI
I want to rearrange the elements of an array in a certain order,based on their index.
For example:
Input array A = [1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 2]
Output array B should look like this: B = [1111111122222222]
The values in the array are random, so I cannot search for a "1" or "2".
(I will always have them in blocks of 4)
Your description is not specific enough to provide a generic solution. Are there always only two different choices. Is the array lenght always an integer multiple of 8? HOw long is the input array?
What should happen if the input array is [111122221111222211112222]?
What if it is [111122223333111122223333]?
09-19-2013 03:50 PM
Sorry here is a better description:
The input array is actually 128 elements.
The value of the array is not important, and I dont want to sort based on that. It shouldbe sorted based on the index.There are only 2 cases, so its like I need to switch the order of elements in each block of 4.
Assuming the input has 16 length:
[0][1]...[15]
the output should be in the following order, I used the [] to show the element at that index:
[0][1][2][3][8][9][10][11][4][5][6][7][12][13][14][15] and so on...
09-19-2013 04:25 PM - edited 09-19-2013 04:25 PM