How can I rotate a matrix 45 degrees?
124 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Connor Mondock
le 18 Fév 2022
Commenté : Alan Keenan
le 14 Déc 2022
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?
0 commentaires
Réponse acceptée
Kevin Holly
le 18 Fév 2022
Modifié(e) : Kevin Holly
le 18 Fév 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 commentaires
Kevin Holly
le 18 Fév 2022
Did you save the plot with the variable name p?
p = plot(C(1,:),C(2,:));
Plus de réponses (1)
Jan
le 18 Fév 2022
Modifié(e) : Jan
le 10 Déc 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]
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 commentaires
Jan
le 10 Déc 2022
Modifié(e) : Jan
le 10 Déc 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
le 14 Déc 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.
Voir également
Catégories
En savoir plus sur Delaunay Triangulation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!