Home > front end >  How to change the dimension of a numpy array
How to change the dimension of a numpy array

Time:09-30

I have an array of 2-d values created through a mesh grid. The arrays are returned as 40X40 objects that I want to transform to 2X1 array in order to perform multiplication with 2X2 matrix.

A=np.array([[0,-1],[1,0]])
x, y = meshgrid(arange(-2, 2, 0.1), arange(-2, 2, 0.1))
X=np.array([[x], [y]])

np.matmul(A,X)

I want the multiplication to return a 2X1 vector with the first component being all the -y values and the second being x values.

EDIT: I want to point out that the matrix multiplication is not absolutely necessary. The point is that I want to dictate whether I get y or x through a matrix which also dictates the sign of the values and their magnitude.

CodePudding user response:

Your array's shape will be 40x1 not 40x40. It cannot be converted into 2x1. But it can converted into the multiples of 40 shape. Like 8x5,20x2,10x4 etc.

CodePudding user response:

You can reshape the 3D 2x40x40 array to be a 2D 2x1600 array, and use matmul on that, reshaping the result back afterwards:

X = np.array((x, y)).reshape(2, 1600)
A = np.array([[0,-1],[1,0]])
Y = (A @ X).reshape(2, 40, 40)
  • Related