1

First time i ask something here. I am kind of 'blocked'.

I have an array composed of n x n arrays (lets take n=3 for simplification):

[
    [
        [ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]
    ],
   [
        [9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]
    ],
   [
        [18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]
    ]
        ]

(altho my array contains a lot more than 3 3*3 arrays)

i want to achieve a 2D array like this :

[0,1,2,9,10,11,18,19,20]  
[3,4,5,12,13,14,21,22,23]    
[6,7,8,15,16,17,24,25,26]

Is there a trick i haven't thought of because i cant think of any way to accomplish the transformation.

Thanks

0

4 Answers 4

2

Slightly cleaner than moveaxis maybe:

import numpy as np
a = np.arange(27).reshape(3,3,3)
a.swapaxes(0,1).reshape(3,-1)
array([[ 0,  1,  2,  9, 10, 11, 18, 19, 20],
   [ 3,  4,  5, 12, 13, 14, 21, 22, 23],
   [ 6,  7,  8, 15, 16, 17, 24, 25, 26]])
Sign up to request clarification or add additional context in comments.

Comments

1

Think of this as a list of 3 arrays, that you want to join horizontally:

In [171]: arr = np.arange(27).reshape(3,3,3)
In [172]: np.hstack(arr)
Out[172]: 
array([[ 0,  1,  2,  9, 10, 11, 18, 19, 20],
       [ 3,  4,  5, 12, 13, 14, 21, 22, 23],
       [ 6,  7,  8, 15, 16, 17, 24, 25, 26]])

In [173]: arr
Out[173]: 
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])

Of I like to test ideas with arrays with differing dimensions. Then the various axis manipulations becomes more obvious.

In [174]: arr = np.arange(24).reshape(2,3,4)
In [175]: arr
Out[175]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
In [176]: np.hstack(arr)
Out[176]: 
array([[ 0,  1,  2,  3, 12, 13, 14, 15],
       [ 4,  5,  6,  7, 16, 17, 18, 19],
       [ 8,  9, 10, 11, 20, 21, 22, 23]])

In [177]: np.vstack(arr)
Out[177]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])

But there's nothing wrong with the variations on the transpose and reshape answers, if starting from a 3d array (as opposed to a list of arrays):

In [187]: arr.transpose(1,0,2).reshape(3,-1)
Out[187]: 
array([[ 0,  1,  2,  9, 10, 11, 18, 19, 20],
       [ 3,  4,  5, 12, 13, 14, 21, 22, 23],
       [ 6,  7,  8, 15, 16, 17, 24, 25, 26]])

2 Comments

should be probably np.stack instead
@YannicHamann I initially suggested stack, but in this case it doesn't help. hstack or `concatenate on axis 1 is so much simpler.
1

You could use np.block

>>> import numpy as np
>>> X = np.arange(27).reshape(3, 3, 3)
>>> 
>>> np.block(list(X))
array([[ 0,  1,  2,  9, 10, 11, 18, 19, 20],
       [ 3,  4,  5, 12, 13, 14, 21, 22, 23],
       [ 6,  7,  8, 15, 16, 17, 24, 25, 26]])

Comments

0

A simple reshape doesn't suffice since you have to change the order of the axes first:

import numpy as np
a = np.array([[[0,1,2],[3,4,5],[6,7,8]],[[9,10,11],[12,13,14],[15,16,17]],[[18,19,20],[21,22,23],[24,25,26]]])
np.moveaxis(a, 0, 1).reshape(3,9)

[[ 0  1  2  9 10 11 18 19 20]
 [ 3  4  5 12 13 14 21 22 23]
 [ 6  7  8 15 16 17 24 25 26]]

1 Comment

thank you very much ascripter. I hadnt read about moveaxis. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.