Reputation: 187
I am getting a not aligned shapes error in python and am not sure how to resolve it. The goal is to take the dot product between an array and then the rotation matrix.
I am calling two text file lists and converting them to arrays. Am I not converting the two lists to arrays and then to matrix form correctly?
x=dataset[:,0]
y=dataset[:,1]
z_nodatum=dataset[:,2]
stage=dataset[:,17]
amp=dataset[:,5]
x=np.array(x)
y=np.array(y)
z=-(z_nodatum-2919)
z=np.array(z)
A = np.column_stack(([x],[y]))
theta = np.radians(20)
c,s =np.cos(theta), np.sin(theta)
R = np.matrix('{} {};{} {}' .format(c, -s, s, c))
B=A*R
Also I would then like to take B and convert it back to the x and y list for plotting.
Thanks for the help.
Upvotes: 0
Views: 600
Reputation: 4902
I'm guessing your dataset
array is a 2D array, and not only that, it probably is already a numpy array since you can't index normal python arrays via [:(tuple)]
given that, it appears that these five lines
x=dataset[:,0]
y=dataset[:,1]
z_nodatum=dataset[:,2]
stage=dataset[:,17]
amp=dataset[:,5]
actually correspond to specified columns in the dataset
. x
, y
, z_notatum
, stage
, and amp
are already 1xN arrays.
Given this, the lines:
x=np.array(x)
y=np.array(y)
Do nothing, x and y are already numpy arrays. z=-(z_nodatum-2919)
subtracts 2919 from every element, negates the result and returns the resulting numpy array, if this is actually what you wanted. But again, z=np.array(z)
doesn't do anything, you already had a numpy array in the first place.
This next line is probably also something I don't think you want to do.
A = np.column_stack(([x],[y]))
What this does is it takes the array of the 1xN arrays x
and y
, and column stacks them, which ends up being [[x1, x2... xn, y1, y2...yn]]
. What you probably wanted was:
A = np.column_stack((x,y))
which returns a 2D numpy array of columns x
and y
[[x1,y1],[x2, y2]...[xn,yn]]
. Note that if this is all you were going to do with x and y you could have done this in the beginning:
A = dataset[:,:2]
This would have given you x and y together in the first place if they were right next to each other in dataset
(takes 0 -> n-1 columns, where n here is 2)
Your rotation matrix appears valid, but note, you probably should have created it via:
theta = np.radians(20)
cos_theta,sine_theta =np.cos(theta), np.sin(theta)
R = np.matrix([[cos_theta, -sine_theta], [sine_theta, cos_theta]])
You only use the string format version when it is more convenient (ie reading text from elsewhere) or you have some complicated structure that necessitates it. That is not the case here.
B=A*R
in your version you have a 1x1xN np array for A, and a 2x2 for R. This obviously isn't going to work.
With the revised changes I suggest, you would get a row x column, Nx2 * 2x2 operation, which is valid since the inner sizes match. To get the rotated x
and y
out of B
, you can do what you were doing in the beginning, B
is now a Nx2 numpy array at this point (A*B = C, size of C is A.rows X B.cols, outer size values):
x_rotated = B[:,0]
y_rotated = B[:,1]
Now everything should work. Be careful with matrix multiplication in numpy though, make sure at least one of your values is a matrix, otherwise you will be doing element wise multiplication (your R
is a matrix). Additionally order of operations may make two arrays in your equation (ie array * array * matrix) perform element wise multiplication before your matrix operation.
Upvotes: 1