How can I rotate a matrix 45 degrees?

80 ビュー (過去 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 Center および File ExchangeDelaunay Triangulation についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by