How to rotate a matrix?

95 次查看(过去 30 天)
Hossein
Hossein 2018-4-26
评论: Rik 2018-4-30
Hi guys
Assume we have a 2D matrix A1 which includes x and y locations. I have calculated the values related to each (x,y) of matrix A1 and then put them in another matrix of B1, with the same dimension as A1. In other words, I have a 3D matrix that I have defined it like this.
Now, I need to rotate this 3D matrix around z axis and add it to the previous one, not rotated one, I am confused how to do this. The problem is that this rotation, changes, of course, x and y locations, so I cannot simply add two matrices because the new matrix has different x and y locations. But B1 stays the same. Can you please help me figure this out?
Thanks
  3 个评论
Hossein
Hossein 2018-4-26
That is a very interesting answer. How can I use explicit coordinates? I have not tried this before.
Hossein
Hossein 2018-4-26
The only way I know to define a coordinate is meshgrid. And if I do so, I need to rotate it and then how to add relevent new (x,y)s to the previous ones?

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-4-26
You need to apply a conversion to the values inside the matrix, not the matrix itself. Using meshgrid is indeed helpful for generating explicit coordinates.
You could even use arrayfun and use a normal rotation matrix (untested code):
[X,Y]=meshgrid(x_coordinates,y_coordinates);
Z=some_function(X,Y);
[new_X,new_Y,new_Z]=arrayfun(@apply_transform,X,Y,Z);
function [new_x,new_y,new_z]=apply_transform(x,y,z)
R=eye(4);%replace by actual transform matrix
v=[x;y;z;1];
new_v=R*v;
new_x=new_v(1);
new_y=new_v(2);
new_z=new_v(3);
end
  3 个评论
Rik
Rik 2018-4-27
Each position in the matrices X,Y,Z is a single point, where each matrix contains one coordinate element. You can choose to overwrite the old matrices with the new coordinates, but that is your choice. I chose to write it this way to clarify where the change happens.
I don't really understand what you mean with how A1 is structured, but this point is connected to your third question. To facilitate not only rotation and translation, but also scaling, the transformation matrices are sometimes written as a (dimension+1)-by-(dimensions+1) matrix. The (end,end) position is 1 if no scaling should be applied. The rest of the last col/row contain only zeros. If you choose to use this structure, your vector to be transformed is [x;y;z;1].
As for the last thing you mention: read the documentation for arrayfun. X,Y,Z are array inputs, and the function is applied on each position separately, so inside the function x,y,z are scalar. That is the reason for my choice in upper/lower case variable names.
Rik
Rik 2018-4-30
Did my answer solve your problem? If so, please mark it as accepted answer, if not, feel free to comment with your remaining questions.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by