MATLAB - How to rotate an orbit / sphere using a rotation matrix?
I am trying to rotate an "orbit" using the rotation matrix below:
[cos(angle) -sin(angle) 0;
sin(angle) cos (angle) 0;
0 0 1 ]
The first thing I thought I should have done was use a sphere ():
[x y z] = sphere;
Then concatenate x, y and z together in a vector:
xyz = [x; y; z];
rotatMat = [cos(angle) -sin(angle) 0; sin(angle) cos (angle) 0; 0 0 1 ];
multiply the rotation matrix and xyz by the rotation of the orbit:
rotated = rotatMat .* xyz;
However, xyz
it turns out to be the size 62x22
, and mine rotatMat
is only 3x3
, so I cannot reproduce them together.
How can I fix this problem?
Thanks in advance.
source to share
You should use the operator *
for matrix multiplication, not the .*
one that is for elementwise multiplication.
Also, your matrix xyz
must be sized n-by-3
(not 62-by-22
) and you must use xyz*rotatMat'
to match the dimensions correctly. Alternatively, you can be xyz
sized 3-by-n
and using the syntax rotatMat*xyz
.
Best,
source to share
xyz = [x(:) y(:) z(:)].'; %'// put x, y, z as rows of a matrix
xyz_rotated = rotatMat*xyz %// use matrix multiplication
x_rotated = reshape(xyz_rotated(1,:), size(x)); %// reshape transformed rows
y_rotated = reshape(xyz_rotated(2,:), size(x));
z_rotated = reshape(xyz_rotated(3,:), size(x));
source to share