Cupitor
Cupitor

Reputation: 11637

Transposing arrays in an array

I have a 2D array of shape (M*N,N) which in fact consists of M, N*N arrays. I would like to transpose all of these elements(N*N matrices) in a vectorized fashion. As an example,

import numpy as np
A=np.arange(1,28).reshape((9,3))
print "A before transposing:\n", A
for i in range(3):
    A[i*3:(i+1)*3,:]=A[i*3:(i+1)*3,:].T
print "A after transposing:\n", A

This code generates the following output:

A before transposing: 
[[ 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]]
A after transposing: 
 [[ 1  4  7]
 [ 2  5  8]
 [ 3  6  9]
 [10 13 16]
 [11 14 17]
 [12 15 18]
 [19 22 25]
 [20 23 26]
 [21 24 27]]

Which I expect. But I want the vectorized version.

Upvotes: 5

Views: 169

Answers (2)

YXD
YXD

Reputation: 32511

Here's a nasty way to do it in one line!

A.reshape((-1, 3, 3)).swapaxes(-1, 1).reshape(A.shape)

Step by step. Reshape to (3, 3, 3)

>>> A.reshape((-1, 3, 3))
array([[[ 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]]])

Then perform a transpose-like operation swapaxes on each sub-array

>>> A.reshape((-1, 3, 3)).swapaxes(-1, 1)
array([[[ 1,  4,  7],
        [ 2,  5,  8],
        [ 3,  6,  9]],

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

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

Finally reshape to (9, 3).

>>> A.reshape((-1, 3, 3)).swapaxes(-1, 1).reshape(A.shape)
array([[ 1,  4,  7],
       [ 2,  5,  8],
       [ 3,  6,  9],
       [10, 13, 16],
       [11, 14, 17],
       [12, 15, 18],
       [19, 22, 25],
       [20, 23, 26],
       [21, 24, 27]])
>>> 

I think that with any method, data must be copied since there's no 2d strides/shape that can generate the result from:

array([ 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])

(is there?) In my version I think data is copied in the final reshape step

Upvotes: 8

Abid Rahman K
Abid Rahman K

Reputation: 52646

In [42]: x = np.arange(1,28).reshape((9,3))

In [43]: x
Out[43]: 
array([[ 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]])


In [31]: r,c = x.shape
In [39]: z = np.vstack(np.hsplit(x.T,r/c))

In [45]: z
Out[45]: 
array([[ 1,  4,  7],
       [ 2,  5,  8],
       [ 3,  6,  9],
       [10, 13, 16],
       [11, 14, 17],
       [12, 15, 18],
       [19, 22, 25],
       [20, 23, 26],
       [21, 24, 27]])

Upvotes: 3

Related Questions