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