JayJay123
JayJay123

Reputation: 313

Python NumPy Convert Columns to Rows

Python 2.7.10 and NumPy. I have a matrix like this:

[[[ 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]
[27 28 29]
[30 31 32]
[33 34 35]]

[[36 37 38]
[39 40 41]
[42 43 44]
[45 46 47]]]

Note: The real matrix will have real data, and not consecutive numbers.

I need to rotate, flip, or something (I have tried them all) so as to end up with this:

[[[ 2 5 8 11]
[ 1 4 7 10]
[ 0 3 6 9]

[[14 17 20 23]
[13 16 19 22]
[12 15 18 21]

[[26 29 32 35]
[25 28 31 34]
[24 27 30 33]

[[38 41 44 47]
[37 40 43 46]
[36 39 42 45]]]

Basically, I need the entire columns of the matrix to become the rows.

Thanks.

Upvotes: 3

Views: 7964

Answers (4)

gbronner
gbronner

Reputation: 1945

Here's a simpler way to do it:

a=numpy.arange(48).reshape((4,4,3)
numpy.fliplr(a.swapaxes(1,2))
#or you could do
numpy.fliplr(a.transpose(0,2,1))

From what I can tell, flipud flips the last dimension, while fliplr flips the second to last dimension. In three dimensions, the last dimension is Z, while the second to last dimension is Y. Hence transposing the data, and flipping the Y dimension works.

Enjoy.

Upvotes: 2

gbronner
gbronner

Reputation: 1945

transpose and flipud are what you are looking for; the swapaxes can also function as transpose Note that transpose has a version that operates on multiple dimensions.

There may be a simpler expression for this, but this has the advantage of not using elaborate indexing. Example, done in Python 2.7.3 with numpy

f=numpy.flipud
a=numpy.arange(48).reshape((4,4,3))
result=f(f(f(a).T).T).transpose(0,2,1)

In [2]: a=numpy.arange(48).reshape((4,4,3))
Out[3]:
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],
    [27, 28, 29],
    [30, 31, 32],
    [33, 34, 35]],

   [[36, 37, 38],
    [39, 40, 41],
    [42, 43, 44],
    [45, 46, 47]]])
In [5]: f(f(f(a).T).T).transpose(0,2,1)
Out[5]:
array([[[ 2,  5,  8, 11],
    [ 1,  4,  7, 10],
    [ 0,  3,  6,  9]],

   [[14, 17, 20, 23],
    [13, 16, 19, 22],
    [12, 15, 18, 21]],

   [[26, 29, 32, 35],
    [25, 28, 31, 34],
    [24, 27, 30, 33]],

   [[38, 41, 44, 47],
    [37, 40, 43, 46],
    [36, 39, 42, 45]]])

.

Upvotes: 0

Divakar
Divakar

Reputation: 221574

Flip the positions of columns with [:,:,::-1] and use np.transpose to swap rows with columns -

In [25]: A
Out[25]: 
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],
        [27, 28, 29],
        [30, 31, 32],
        [33, 34, 35]]])

In [26]: A[:,:,::-1].transpose(0,2,1)
Out[26]: 
array([[[ 2,  5,  8, 11],
        [ 1,  4,  7, 10],
        [ 0,  3,  6,  9]],

       [[14, 17, 20, 23],
        [13, 16, 19, 22],
        [12, 15, 18, 21]],

       [[26, 29, 32, 35],
        [25, 28, 31, 34],
        [24, 27, 30, 33]]])

Upvotes: 3

NJM
NJM

Reputation: 595

For each 2d subarray in your super-array you can apply the numpy function:

np.rot90() http://docs.scipy.org/doc/numpy/reference/generated/numpy.rot90.html

so:

import numpy as np

array= 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],
[27, 28, 29],
[30, 31, 32],
[33, 34, 35]],

[[36, 37, 38],
[39, 40, 41],
[42, 43, 44],
[45, 46, 47]]])

desired_output = np.array([np.rot90(sub_array) for sub_array in array])

Upvotes: 1

Related Questions