x and y must have same first dimension, but have shapes (2700,) and (200,)

Viewed 50

I have a NumPy array (2,4) and I want to rotate it 90 degrees to the left and I use np.rot90

For example, if my array is

x = [[5,6,7,8],
     [1,2,3,4]]

and when I rotate, it will be

y = [[8,4],
     [7,3],
     [6,2],
     [5,1]]
                                 

x is (2,4) NumPy array and y will be (4,2)

y = np.rot90(x)

But when I run this code I get this failure

x and y must have the same first dimension, but have shapes (4,) and (2,)
1 Answers

Not that computionally beautiful but working:

import numpy as np

x = [[5,6,7,8],

     [1,2,3,4]]

​

y = np.zeros((len(x[0]),len(x)), dtype = int)

​

for i,j,k in zip(x[0], x[1], range (0,len(x[0]))):

    y[len(x[0])-1-k] = [i,j]

    np.asmatrix(y)

print (y)

[[8 4],
 [7 3],
 [6 2],
 [5 1]]
Related