MATLAB: Calculate Rotation Matrix from vector in one cooridnate system to that vector in another coordinate system.

linear algebramatrix operationsrotation matrix

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 3×3 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

Best Answer

However, I need to create a MATLAB code that, between any two coordinate systems, can calculate the rotation matrix.
You need at least 3 vectors in each system to determine a coordinate rotation. Once you have three vectors, you can use this FEX file
to get the rotation matrix.