1

Below I have two different sets of code trying to do the same thing. I want to be able to take many poles[n, 3] and create a rotation matrix for each omega [n x 1]. I am having a difficult time trying to figure out how I am supposed to go about storing and manipulating the rotation matrices.

I have tried also doing this with 3D matrices but run into the problem of some functions not supporting a 3D matrix.

function [R] = rotationMatrix(pole,omega)
% omega = degree of rotation
% pole = x y z vector signifying pole to rotate about
% Pole [x y z] and omega are in radians 
Ex = pole(:,1);
Ey = pole(:,2);
Ez = pole(:,3);

%% 
% R11 = Ex.*Ex.*(1-cos(omega))+cos(omega);
% R12 = Ex.*Ey.*(1-cos(omega))-Ez*sin(omega);
% R13 = Ex*Ez*(1-cos(omega))+Ey*sin(omega);
% 
% R21 = Ey*Ex*(1-cos(omega))+Ez*sin(omega);
% R22 = Ey*Ey*(1-cos(omega))+cos(omega);
% R23 = Ey*Ez*(1-cos(omega))-Ex*sin(omega);
% 
% R31 = Ez*Ex*(1-cos(omega))-Ey*sin(omega);
% R32 = Ez*Ey*(1-cos(omega))+Ex*sin(omega);
% R33 = Ez*Ez*(1-cos(omega))+cos(omega);

R11 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ex,Ex),bsxfun(@minus,1,cos(omega))),cos(omega));
R12 = bsxfun(@minus,bsxfun(@times,bsxfun(@times,Ex,Ey),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ez,sin(omega)));
R13 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ex,Ez),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ey,sin(omega)));

R21 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ey,Ex),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ez,sin(omega)));
R22 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ey,Ey),bsxfun(@minus,1,cos(omega))),cos(omega));
R23 = bsxfun(@minus,bsxfun(@times,bsxfun(@times,Ey,Ez),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ex,sin(omega)));

R31 = bsxfun(@minus,bsxfun(@times,bsxfun(@times,Ez,Ex),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ey,sin(omega)));
R32 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ez,Ey),bsxfun(@minus,1,cos(omega))),bsxfun(@times,Ex,sin(omega)));
R33 = bsxfun(@plus,bsxfun(@times,bsxfun(@times,Ez,Ez),bsxfun(@minus,1,cos(omega))),cos(omega));

R = [R11 R12 R13;
    R21 R22 R23;
    R31 R32 R33];
end

Edited with advice from comments

For example if I have a 2x3 matrix of vectors

     [x y z] = sph2cart(deg2rad(312),deg2rad(-37),1)
poles(:,:,1) = [ x, y, z];
poles(:,:,2) = [0.4, -0.3, 0.1];
poles(:,:,3) = [-0.1, 0.4, -0.5];

And a 1x3 matrix of angles by which I want to rotate

omega(:,:,1) = degtorad(65);
omega(:,:,2) = 0.92;
omega(:,:,3) = 0.48;

And run the function rotationMatrix(poles, omega)

R = rotationMatrix(poles, omega)

I get this matrix of values in the form of 3x3x3

R(:,:,1) =

     0.587503610427254          0.36230591007508         -0.72358408997131
    -0.728553373601606         0.625997769999203        -0.278094900653973
     0.352206600660266         0.690551388008664         0.631735143054941
etc...

Now I want to rotate some coordinates with this matrix 3x1x3

A(:,:,1) = [-0.604 0.720 0.342]';
A(:,:,2) = [-0.604 0.720 0.342]';
A(:,:,3) = [-0.604 0.720 0.342]';

Now I am going to rotate these points but it appears I can't do matrix multiplication..

>> Ap = R*A
Error using  * 
Inputs must be 2-D, or at least one input must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.
 
>> Ap = R.*A
Array dimensions must match for binary array op.

Using bsxfun also doesn't work because it returns a 3x3 matrix, when it should be 3x1

Ap = bsxfun(@times,R,A)

Ap(:,:,1) =

    -0.354852180698061        -0.218832769685349         0.437044790342671
    -0.524558428993156         0.450718394399426        -0.200228328470861
     0.120454657425811         0.236168574698963          0.21605341892479
etc...

My immediate solution is to construct a for loop. It probably isn't the most efficient method.

for m = 1:length(poles)
    Ap(m,:) = R(:,:,m)*A(:,:,m);
end

How do I go about doing matrix multiplication in 3D space?

Cheers, Daniel

Community
  • 1
  • 1
Daniel
  • 121
  • 1
  • 4
  • what are Pole and Omega? – NKN Feb 18 '14 at 15:54
  • rotation in 3D has 3 different matrices for 3 axis. So if you have your angles of rotation about each of the axes. you make your matrices. and then you make the final rotation matrix by multiplying these 3 matrices. Finally when you have your rotation matrix, if you multiply a 3D point by this matrix you will get a new point which is the result of the rotation. – NKN Feb 18 '14 at 15:56
  • Your `rotationMatrix()` looks fine. What is the problem here? It looks like a Rodrigues 3×3 rotation matrix composition from axis angle. – John Alexiou Feb 18 '14 at 16:49
  • The rotation matrix works fine if I only use one pole and one angle. I would like to be able to create a function where I can have multiple poles and multiple angles of rotation. I added some revised code and some results that display some issues I am having. – Daniel Feb 18 '14 at 17:36
  • Thanks for the response @NKN . So I was able to calculate a Rotation Matrix in 3D space. Now I am having difficulty multiplying 3D matrices together. I added my issue at the bottom of the comment. – Daniel Feb 18 '14 at 19:46
  • do it easily like this: R1*R2*R3 each of these are 3x3 matrices. if you have a 3x3x3 then do this: c(:,:,1)*c(:,:,2)*c(:,:,3) – NKN Feb 18 '14 at 19:48
  • You won't happen to have a license to the symbolic toolbox? What you are trying to do is define a symbolic rotation matrix, but without symbolics. Please show why the simple `rotationMatrix()` function won't do. – John Alexiou Feb 18 '14 at 20:11
  • I still don't see what the problem is. Once you have the rotation matrices sequence you multiply them with vectors to get the 3×1 result. For example `v = R1*R2*R3*u`. – John Alexiou Feb 18 '14 at 20:15
  • I guess I don't understand what you (@ja72) and @NKN are referring to when you state R1*R2*R3*u. What is R1 and R2 referring to? – Daniel Feb 18 '14 at 20:30
  • And I do not have access to the Symbolic Math Toolbox. – Daniel Feb 18 '14 at 20:36
  • R1 and R2 are the 3×3 rotation matrices as outputed by each call to `rotationMatrix()` for each set of axis/angle values. – John Alexiou Feb 18 '14 at 20:50

0 Answers0