# calculate rotation matrix in 3D

16 views (last 30 days)
bbah on 7 Jan 2020
Commented: bbah on 10 Jan 2020
i have 3 axis starting at a point of origin and a rotated vector dir1_new starting at the origin. How can i calculate the rotation matrix =
E.G
orig = [311.51 -23.0379 -448.7862]
axis_1 = [2.7239 -0.2014 -3.9228] % starts at origin
axis_2 = [-0.4315 -5.8348 9.9199e-06] % starts at origin
axis_3 = [4.4008 -0.3255 3.0715] % starts at origin
my new vector is :
dir1_new = [2.6843 -0.1997 -3.9435] % starts at origin
bbah on 8 Jan 2020
The three vectors are already unit vectors. Should i just negate axis 3 ? And how to calculate then the rotations ?thank you
Jim Riggs on 8 Jan 2020
No, these are not unit vectors. Their magnitudes are all greater than 1.

Jim Riggs on 8 Jan 2020
Edited: Jim Riggs on 8 Jan 2020
I'll assume that we make the reference frame right-handed by negating the axis_3 vector such that
axis_1 = [2.7239 -0.2014 -3.9228];
axis_2 = [-0.4315 -5.8348 9.9199e-6];
axis_3 = [-4.4008 0.3255 -3.0715];
% Now vector r is rotated from axis_1 to
dir1 = [2.6843 -0.1997 -3.9435]
In the above vectors, I have flipped the sign on axis_3, but kept dir1 the same.
I can't tell if it is also appropriate to flip the sign on the Z-component for dir1, i.e.
dir1 = [2.6843 -0.1997 3.9435];
You will have to decide if this makes sense based on your problem and this is what you realy want.
Now I want to describe the dir1 vector in the user-specified coordinate frame defined by axis_1, axis_2, and axis_3.
I will construct unit vectors for each axis (as in my comment above):
Vmag = @(x) sqrt(sum(x.^2)));
U1 = axis_1./Vmag(axis_1);
U2 = axis_2./Vmag(axis_2);
U3 = axis_3./Vmag(axis_3);
Now project vector dir1 onto the user axes. I call this projected vector r:
r = [dot(dir1,U1) dot(dir1,U2) dot(dir1,U3)];
Vector r is vector dir1 expressed in the user-defined axes (axis_1, axis_2, axis_3).
I also want a unitized vector for the direction:
Ur = r./Vmag(r);
Now Ur is the unit vector in the "r" direction, expressed in the user reference frame, such that r(1) {and Ur(1)} is along axis_1, r(2) { and Ur(2)} is along axis_2, and r(3) { and Ur(3)} is along axis_3.
Now, my understanding of your original question is that this unit vector, Ur, represents a rotation from U1, so you want to know how to find the rotation matrix that will transform U1 to Ur, i.e you want matrix C such that [C] U1 = Ur
However, in your most recent comment, you say you want the angles based on projections in the three planes of the reference frame. So these are not the same thing.
However, once you have Ur, you have the vector projetions of dir1 along all 3 of the user-defined axes, so it is easy to compute angles in this frame from these components. Again, you will need to be more clear on what it is that you are wanting.
Jim Riggs on 10 Jan 2020
Edited: Jim Riggs on 10 Jan 2020
No.
Vector r represents the transformation of dir1 into the user coordinate frame. If dir1 is already defined in this frame, then you do not need to transform it.
bbah on 10 Jan 2020
Got it. Thank you a lot

Matt J on 10 Jan 2020
Edited: Matt J on 10 Jan 2020
B1=[axis_1/norm(axis_1);axis_2/norm(axis_2);axis_3/norm(axis_3)];
B2=[dir1_new(:),null(dir1_new)];
s=sign(det(B2));
B2=B2.*[1,1,s];
rotationMatrix=B2*B1;