How can I rotate a matrix 45 degrees?

조회 수: 42 (최근 30일)
Connor Mondock
Connor Mondock 2022년 2월 18일
댓글: Alan Keenan 2022년 12월 14일
I want to rotate this matrix by 45 degrees:
C = [-6 -6 -7 0 7 6 6 -3 -3 0 0 -6; -7 2 1 8 1 2 -7 -7 -2 -2 -7 -7]
C = 2×12
-6 -6 -7 0 7 6 6 -3 -3 0 0 -6 -7 2 1 8 1 2 -7 -7 -2 -2 -7 -7
plot(C(1,:),C(2,:)), xlim([-10 10]), ylim([-10 10])
It plots a simple house shape. I want to take this matrix, or the house rather, and rotate it 45 degrees, and then flip it after the rotation. How would I do that?

채택된 답변

Kevin Holly
Kevin Holly 2022년 2월 18일
편집: Kevin Holly 2022년 2월 18일
C = [-6 -6 -7 0 7 6 6 -3 -3 0 0 -6; -7 2 1 8 1 2 -7 -7 -2 -2 -7 -7];
p = plot(C(1,:),C(2,:)); xlim([-10 10]); ylim([-10 10]);
rotate(p,[0,0,1],45)
figure
plot(-p.XData,p.YData)
xlim([-10 10])
ylim([-10 10])
  댓글 수: 4
Kevin Holly
Kevin Holly 2022년 2월 18일
Did you save the plot with the variable name p?
p = plot(C(1,:),C(2,:));
Connor Mondock
Connor Mondock 2022년 2월 18일
No I used D instead that was an easy fix thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2022년 2월 18일
편집: Jan 2022년 12월 10일
C = [-6 -6 -7 0 7 6 6 -3 -3 0 0 -6; -7 2 1 8 1 2 -7 -7 -2 -2 -7 -7]
C = 2×12
-6 -6 -7 0 7 6 6 -3 -3 0 0 -6 -7 2 1 8 1 2 -7 -7 -2 -2 -7 -7
plot(C(1,:),C(2,:));
xlim([-10 10]);
ylim([-10 10]);
axis equal
hold on;
% Rotate coordinates by 45 deg clockwise:
D = [cosd(45), sind(45); -sind(45), cosd(45)] * C; % [EDITED, Typo fixed, thanks Alan]
plot(D(1,:), D(2,:), 'r');
Mirroring the y coordinates is a multiplication by [1, 0; 0, -1].
  댓글 수: 5
Jan
Jan 2022년 12월 10일
편집: Jan 2022년 12월 10일
@Alan Keenan: "Why is the last term in the rotation matrix cos rather than cosd?" - This is called a typo. Thanks for finding it. The code is fixed now.
You have tried what? Please post your code instead of letting me guess, what "multiply x,y combination" exactly means.
% Maybe:
x = -14e-3:1e-3:14e-3;
y = 20e-3:2e-3:34e-3;
R = [cosd(45), sind(45); -sind(45), cosd(45)];
axes('NextPlot', 'add'); % as: hold('on')
for ix = 1:numel(x)
for iy = 1:numel(y)
plot(x(ix), y(iy), 'ro');
xy2 = R * [x(ix); y(iy)];
plot(xy2(1), xy2(2), 'bo');
end
end
Now you ask, why all rotated X and Y coordinates of your example are positive, but with my example data, there are negative values also?
Well, isn't this trivial? A rotation around the origin by 45° moves some points to the 1st quadrant, and some not.
Alan Keenan
Alan Keenan 2022년 12월 14일
Thanks for your feedback, I can see now that I need to have my co-ordinates equispaced around the 0,0 origin.
So, instead of x = -14e-3:1e-3:14e-3; y = 20e-3:2e-3:34e-3;
I have used x = -14e-3:1e-3:14e-3; y = -7e-3:2e-3:7e-3;
It gives the following rotation:
I can then add the offset value after the rotation so that I still have the original co-ordinates.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by