LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

rearrange array elements

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!

 

 

0 Kudos
Message 1 of 9
(3,050 Views)

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.

 

Spoiler
Sort 1D array

 

Cameron

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 2 of 9
(3,039 Views)

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.

0 Kudos
Message 3 of 9
(3,031 Views)

You might start with a straight-forward approach:

check.png

😄

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 9
(3,027 Views)

Well I kind of wanted to avoid that!

Is there a more generic solution which I could apply?

0 Kudos
Message 5 of 9
(3,014 Views)

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...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 9
(3,011 Views)

@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]?

0 Kudos
Message 7 of 9
(3,006 Views)

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...

0 Kudos
Message 8 of 9
(2,999 Views)

Let's assume you have a modern LabVIEW version. Here's what you could do.

 

0 Kudos
Message 9 of 9
(2,986 Views)