How to rotate a matrix?
95 views (last 30 days)
Show older comments
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
Accepted Answer
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.
[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
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
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.
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping 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!