How to rotate a matrix?

37 views (last 30 days)
Hossein
Hossein on 26 Apr 2018
Commented: Rik on 30 Apr 2018
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 Comments
Hossein
Hossein on 26 Apr 2018
That is a very interesting answer. How can I use explicit coordinates? I have not tried this before.
Hossein
Hossein on 26 Apr 2018
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?

Sign in to comment.

Accepted Answer

Rik
Rik on 26 Apr 2018
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 Comments
Rik
Rik on 27 Apr 2018
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 on 30 Apr 2018
Did my answer solve your problem? If so, please mark it as accepted answer, if not, feel free to comment with your remaining questions.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!