How can I rotate a matrix 45 degrees?

164 views (last 30 days)
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?

Accepted Answer

Kevin Holly
Kevin Holly on 18 Feb 2022
Edited: Kevin Holly on 18 Feb 2022
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 Comments
Kevin Holly
Kevin Holly on 18 Feb 2022
Did you save the plot with the variable name p?
p = plot(C(1,:),C(2,:));
Connor Mondock
Connor Mondock on 18 Feb 2022
No I used D instead that was an easy fix thanks

Sign in to comment.

More Answers (1)

Jan
Jan on 18 Feb 2022
Edited: Jan on 10 Dec 2022
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 Comments
Jan
Jan on 10 Dec 2022
Edited: Jan on 10 Dec 2022
@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 on 14 Dec 2022
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.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!