From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Make 2D array from 1D array

Solved!
Go to solution

Hi,

 

I have a 1D array and I would like to make a 2D array from it in the following order

 

1D array:  1 2 3 4 5 6 7 8 9 10 11 12

 

if Rows=4 then the 2 D array shoudl look like this           

 

1   5   9  

2   6   10

3   7   11

4   8    12

 

and if Rows=3

 

1   4   7   10

2   5   8    11

3   6   9    12

 

Could you please let me know how cna I do it in LabVIEW?  , Row number and 1D array are the input parameters

 

Thanks

0 Kudos
Message 1 of 9
(4,557 Views)

Reshape Array.

 

You may or may not need to use a Transpose Array after that.

0 Kudos
Message 2 of 9
(4,554 Views)

And what should happen if the number of array elements is not divisible by the number of rows (pad with zeroes? truncate?)

0 Kudos
Message 3 of 9
(4,543 Views)

Thanks OrioleFan

 

Are you sure Reshape array will do that?

 

because if I pass the 1D array and 3 for row to this function it will output     ..  rows dimention =3   column=4

 

1 2 3 4 5 6 7 8 9 10 11 12

it will pass

 

1  2  3  4

5  6  7  8

9 10 11 12

 

but what I expect is this

 

1 4 7 10

2 5 8 11

3 6 9 12

 

 

 

0 Kudos
Message 4 of 9
(4,540 Views)

Hi althebach

 

It is always divisible. 

0 Kudos
Message 5 of 9
(4,535 Views)

To give anything that handles that task well requires an answer to altenbach.

 

You're going to end up wanting to use loops of some type and building your array.  But, you need to know how to handle that corner case.  What do you do if the number of rows s 5?

0 Kudos
Message 6 of 9
(4,530 Views)
Solution
Accepted by topic author tintin_99

As OriolesFan already said, reshape array is the correct function.

 

Here is the more general solution that pads the output array. if the elements are alway evenly divisible, you can leave out the "sign" and "add" function and just wire the IQ output to the upper size terminal of "reshape array". (and generate an error if the remainder output is not zero ;))

 

 

Message 7 of 9
(4,528 Views)

Your solution was given by the first response - you do need to use transpose array.  If you take a twelve element array and reshape to three rows by four columns, this happens

 

1 2 3 4 5 6 7 8 9 10 11 12

 

goes to

 

1   2   3   4

5   6   7   8

9 10 11 12

 

This is not what you want. However, if you reshape to four rows and three columns, you get

 

  1   2   3

  4   5   6

  7   8   9

10 11 12

 

Which is still not what you want, but a Transpose then produces

 

1 4 7 10

2 5 8 11

3 6  9 12

 

which is exactly what you want.

Message 8 of 9
(4,467 Views)

Thanks Altenbach.  I took your code and made it a polymorphic supporting 20 different data types, and added a few more features, and posted it over on LAVA.

 

https://lavag.org/topic/16226-new-array-vi/page-2#entry113339

Message 9 of 9
(4,079 Views)