Calculate Rotation Matrix from vector in one cooridnate system to that vector in another coordinate system.
Show older comments
If I have a vector:
[1, 0, 0]
and I know that when this vector is transformed it becomes:
[0, 1, 0]
I realize this is a rotation about the z-axis of -90 degrees. However, I need to create a MATLAB code that, between any two coordinate systems, can calculate the rotation matrix.
I've read that using Quaternions would be a great way to go, however I have not had a proper linear algebra course and therefore am trying to learn it on my own. My understanding of Quaternions is little to none. Also, my linear algebra is not that great to boot. I am not even sure if I am going about this correctly. I have look at many different Linear Algebra text books, but, of course, none of them show how to get the rotation matrix from the vector.
The code I have written so far looks like this:
function [ R ] = RotationMatrix(vec1, vec2)
x1 = [vec1(:,1), 0, 0];
y1 = [vec1(:,2), 0, 0];
z1 = [vec1(:,3), 0, 0];
x2 = [vec2(:,1), 0, 0];
y2 = [vec2(:,2), 0, 0];
z2 = [ vec2(:,3), 0, 0];
R = [x2;y2; z2]*(inv([x1;y1; z1]))
end
What I tried to do was turn each vector into a 3x3 so that I could take the inverse of the transformed vector and multiply it by the original vector to calculate the rotation matrix.
However, as you can see the test vectors I have chosen will not work because the inverse of those matrices are NaN (not a number).
Does anyone have any tips that could help me with this?
Thank you very much.
Caraline Griffith
1 Comment
Matt J
on 3 Nov 2013
I realize this is a rotation about the z-axis of 90 degrees.
It's also many other things. For example, both of the following rotation matrices map [1 0 0] to [0 1 0]
R1 =
0 0.5000 0.8660
1.0000 0 0
0 0.8660 -0.5000
R2 =
0 0.3420 0.9397
1.0000 0 0
0 0.9397 -0.3420
Accepted Answer
More Answers (0)
Categories
Find more on Quaternion Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!