필터 지우기
필터 지우기

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

조회 수: 8 (최근 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