MATLAB Answers

calculate rotation matrix in 3D

215 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 =
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


Show 3 older comments
Jim Riggs
Jim Riggs on 8 Jan 2020
Need one last point of clarification before an answer may be proposed.
If I interpret the three axes (axis_1, axis_2, and axis_3) to be (X, Y, and Z), then we have a left-handed coordinate frame. Note that if I construct unit vectors from the axis vectors:
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 U1 x U2 = -U3
U2 x U3 = -U1, and
U3 x U1 = -U2
A couple suggested ways to turn this into a propper right-handed system:
1) swap the 1 and 2 axes, or
2) negate axis 3
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
Jim Riggs on 8 Jan 2020
No, these are not unit vectors. Their magnitudes are all greater than 1.

Sign in to comment.

Accepted Answer

Jim Riggs
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.


bbah on 10 Jan 2020
If dir1 is defined in my coordinate system with normalized vectors axis_1 axis_2 and axis_3 do i need to produce vector r ?
Jim Riggs
Jim Riggs on 10 Jan 2020
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

Sign in to comment.

More Answers (1)

Matt J
Matt J on 10 Jan 2020
Edited: Matt J on 10 Jan 2020


Sign in to comment.

Translated by