Calculate Rotation Matrix to align Vector $A$ to Vector $B$ in $3D$? I have one triangle in $3D$ space that I am tracking in a simulation. Between time steps I have the the previous normal of the triangle and the current normal of the triangle along with both the current and previous $3D$ vertex positions of the triangles.
Using the normals of the triangular plane I would like to determine a rotation matrix that would align the normals of the triangles thereby setting the two triangles parallel to each other. I would then like to use a translation matrix to map the previous onto the current, however this is not my main concern right now.
I have found this website http://forums.cgsociety.org/archive/index.php/t-741227.html
that says I must

*

*determine the cross product of these two vectors (to determine a rotation axis)

*determine the dot product ( to find rotation angle)

*build quaternion (not sure what this means)

*the transformation matrix is the quaternion as a $3 \times 3$ (not sure)

Any help on how I can solve this problem would be appreciated.

General solution for n dimensions in matlab / octave:
%% Build input data
n = 4;
a = randn(n,1);
b = randn(n,1);
%% Compute Q = rotation matrix
A =  a*b';
[V,D] = eig(A'+A);
[~,idx] = min(diag(D));
v = V(:,idx);
Q = eye(n) - 2*(v*v');
%% Validate Q is correct
b_hat = Q'*a*norm(b)/norm(a);
disp(['norm of error    = ' num2str(norm(b_hat-b))])
disp(['eigenvalues of Q = ' num2str(eig(Q)')])
