How can I rotate a 3d figure with the rotation matrix without using functions?

6 次查看(过去 30 天)
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?

采纳的回答

Jan
Jan 2022-10-15
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 个评论
Spulber Alexandru
Spulber Alexandru 2022-10-15
What exactly does plot3(G(1, :) does? What does 1,: mean?
Also thank you very much for the response.
Jan
Jan 2022-10-15
编辑:Jan 2022-10-15
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
plot3(X, Y, Z) draws lines in 3D. See: plot3 or type in your command window:
doc plot3

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by