Clear Filters
Clear Filters

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

8 views (last 30 days)
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?

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!