3

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.

0

4 Answers 4

3

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]]])
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I appreciate it. I did try both slicing and transpose, but just not the two together.
I tested this vs the other approach (fliplr(a).transpose(0,2,1), and the f(f(f(a).T).T).transpose(0,2,1), and found that this took 5.3; the fliplr took 8.3, and the last one took 13.6 seconds using the timeit module.
@gbronner Nice! Well I guess it would be interesting to test out A[:,:,::-1].swapaxes(1,2) too!
@Divakar swapaxes seemed to be indistinguishable from transpose(0,2,1).
@gbronner Yes, functionally they are same!
2

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.

Comments

1

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])

Comments

0

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]]])

.

3 Comments

Did not work for me. However, I do appreciate the response.
This does not solve the problem, it transposes the whole array.
Actually, it does work. It is just very ugly. I came up with a simpler version that is clearer. To be perfectly honest, the flipud/fliplr statements seem like they could be replaced with a more generic flip by axis, which would be more obvious as one goes from 2 to 3 dimensions.

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.