rotationVect3D(U, V, M_world)

rotationVect3D using skew-symmetric cross-product matrix
137 Downloads
Updated 12 Mar 2015

View License

This function allows you to find the rotation matrix R that rotates unit vector U onto unit vector V. To achieve this goal, the Skew-symmetric cross product matrix of W is used; where W is the cross product of U and V.
This function is inspired by the discussion on the forum http://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d
Vectors U and V can be none unit vectors, the proposed function takes care of this case.
This method is an efficient and more precise alternative of the use of multiple basis matrices.
This function is also very fast even no mex files are used. For instance, a set of 1.5 million vectors in 3D can be rotated using this function in less than 0.030 seconds on a laptop, (i7-4700MQ CPU @ 2.40GHz, 16.0 GB of Ram)
The function calls two anonymous functions. One that computes the Skew-symmetric cross product, and another that estimates the Rotation Matrix:

% Anonymous functions, compute the Skew-symmetric cross product matrix of v
ssc = @(v) [0 -v(3) v(2); v(3) 0 -v(1); -v(2) v(1) 0];

% Rotation Matrix RU that rotates unit vector "a" onto unit vector "b"
RU = @(A,B) eye(3) + ssc(cross(A,B)) + ssc(cross(A,B))^2*(1-dot(A,B))/(norm(cross(A,B))^2);

Example how to use this function:

% input vectors
inputVect3D = rand(3,1500000);
% even with no unit vector, the function provide the correct matrix
U = [2;0;0];
V = [0;3;0];
tic,rotatedVect3D = rotationVect3D(U, V, inputVect3D ); toc

Cite As

Samir Sahli (2024). rotationVect3D(U, V, M_world) (https://www.mathworks.com/matlabcentral/fileexchange/49986-rotationvect3d-u-v-m_world), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Operators and Elementary Operations in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.2.0.0

Take into account the case where U and V are collinear.
The function will apply an identity matrix on every input vectors

1.1.0.0

* typos in the description has been corrected

1.0.0.0