필터 지우기
필터 지우기

How to rotate a matrix?

조회 수: 31 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by