How can I rotate a 3d figure with the rotation matrix without using functions?
3 views (last 30 days)
Show older comments
I am very new to matlab and i have been trying to rotate a 3d cube around y axis with 45 degrees without using roty or other stuff.
This is the code
x=[2,0,0]
y=[0,0,0]
z=[0,0,2]
o=[0,0,0]
alfa=pi/4
plot3(x,y,z,Color='r')
axis 'equal'
grid
xlabel 'x'
ylabel 'y'
zlabel 'z'
axis 'equal'
t=[1,2];
A = [0 0 0];
B = [1 0 0];
C = [0 1 0];
D = [0 0 1];
E = [0 1 1];
F = [1 0 1];
G = [1 1 0];
H = [1 1 1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
Ry=[cos(alfa),0,sin(alfa);...
0,1,0;...
-sin(alfa),0,cos(alfa)];
G=Ry*P
plot3(G(:,1),G(:,2),G(:,3))
Now, as far as I know i cannot multiply different size matrixes but I dont know how to transform it to have equal sizes.
So how can I transform those matrixes to be equal sizes and would the code make a cube rotate?
0 Comments
Accepted Answer
Jan
on 15 Oct 2022
In a matrix multiplication the length of the row of the first matrix must equal the length of the column of the second one.
In you case transposing the 2nd matrix is enough already:
A = [0 0 0];
B = [1 0 0];
C = [0 1 0];
D = [0 0 1];
E = [0 1 1];
F = [1 0 1];
G = [1 1 0];
H = [1 1 1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
alfa = 45 * pi / 180;
Ry = [cos(alfa),0,sin(alfa);...
0,1,0;...
-sin(alfa),0,cos(alfa)];
G = Ry * P.';
plot3(G(1, :), G(2, :), G(3, :)); % Not G(:, 1)
axis equal
2 Comments
Jan
on 15 Oct 2022
Edited: Jan
on 15 Oct 2022
If G is a matrix, G(1, :) replies the first row, while G(:, 1) replies the first column.
You can learn the basics of Matlab in the "Getting Started" chapters of the documentation and in the free online tutorial: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
doc plot3
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!