3

I am trying to rotate an "orbit" using rotation matrix given below:

[cos(angle) -sin(angle) 0; 

 sin(angle) cos (angle) 0;

 0           0          1 ]

First thing I thought I should do was to use 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 rotation matrix and xyz to rotate an orbit:

rotated = rotatMat .* xyz;

However, xyz turns out to be 62x22 dimension while my rotatMat is only 3x3 so I cannot multiply them together.

How can i fix this issue?

Thank you in advance.

Benoit_11
  • 13,905
  • 2
  • 24
  • 35

2 Answers2

1

You have to use the * operator for matrix multiplication, and not .* which is for element-wise multiplication.

Moreover, your xyz matrix should be of size n-by-3 (and not 62-by-22) and you have to use xyz*rotatMat' to match the dimensions correctly. Alternatively, you can have xyz of size 3-by-n and use the syntax rotatMat*xyz.

Best,

Ratbert
  • 5,463
  • 2
  • 18
  • 37
  • I was typing my answer when I saw this (+1). I hope you don't mind that I've answered more or less the same (but with code) – Luis Mendo Apr 17 '15 at 20:29
  • @LuisMendo No pb for the answer. `xyz` should have 3 columns with the syntax `xyz*rotatMat'` and 3 rows with `rotatMat*xyz`. The output should be the same (but transposed). – Ratbert Apr 17 '15 at 20:36
  • Yes, I realized, that's why I deleted my comment. I'm more used to having the transformation matrix first and the coordinates last :-) – Luis Mendo Apr 17 '15 at 20:39
  • @LuisMendo That's funny, for me it is more natural to have the `xyz` coordinates columnwise, but that's just a matter of taste ! – Ratbert Apr 17 '15 at 20:40
  • Well, I think it's more usual to have a column vector with `[x;y;z]`, where `x`, `y` `z` are numbers. See for example [in Wikipedia](http://en.wikipedia.org/wiki/Transformation_matrix). So if there are several points, each will be a column (`x`, `y`, `z` will be vectors). But of course it can be done the other way around – Luis Mendo Apr 17 '15 at 21:49
0
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));
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147