How can I rotate a matrix 45 degrees?
Show older comments
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]
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
More Answers (1)
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]
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
shubham kumar gupta
on 19 Nov 2022
can you tell for higher than 2x12 ? like 3x12?
Jan
on 20 Nov 2022
See this for general rotation matrices in N-D: https://www.mathworks.com/matlabcentral/fileexchange/66446-rotation-matrix
Alan Keenan
on 9 Dec 2022
D = [cosd(45), sind(45); -sind(45), cos(45)] * C;
Why is the last term in the rotation matrix cos rather than cosd?
I have tried this with:
x = -14e-3:1e-3:14e-3;
y = 20e-3:2e-3:34e-3;
when I multyply the x,y combination with the rotation matrix all of the transformed co-ordinates are positive, in your example there is a mix of positive and negative transformed coordinates, why is this?
thanks
@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
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.
Categories
Find more on Lengths and Angles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



