TITLE: How can I calculate a $4\times 4$ rotation matrix to match a 4d direction vector?
QUESTION [6 upvotes]: I have two 4d vectors, and need to calculate a $4\times 4$ rotation matrix to point from one to the other.
edit - I'm getting an idea of how to do it conceptually: find the plane in which the vectors lie, calculate the angle between the vectors using the dot product, then construct the rotation matrix based on the two. The trouble is I don't know how to mechanically do the first or last of those three steps. I'm trying to program objects in 4space, so an ideal solution would be computationally efficient too, but that is secondary.

REPLY [3 votes]: Here’s a variant on Jyrki Lahtonen's reflection method valid for dimensions $N\ge2$. Let the two unit vectors be $u$ and $v$. Define the reflection function as 
$$
 f(A,n) = A - 2 n  { (n^TA) \over (n^Tn) }
$$
where $A$ is, in general, a matrix and $n$ is the normal of hyperplane $H$. The rotation matrix $R$ is then given by 
\begin{align}
S &= f(I,u+v)\\
R &= f(S, v )
\end{align}
where $I$ is the identity matrix. After the first reflection, $S u = -v$ (and $S v = -u$). The last reflection negates $-v$, giving $v = R u$. This method and the eigenvector approach of user1551 give identical results. 
Perhaps an important feature is that unlike some other methods, $R$ does not rotate vectors orthogonal to $u$ and $v$. QR decomposition of $[u,v]$ gives a square orthonormal matrix Q and an upper triangular matrix $R_t$ such that 
$$
 [u,v] = Q R_t.
$$
The first two columns of $Q$ span $u$ and $v$ and the last $N-2$ columns are orthogonal to $u$ and $v$. Defining Q’ as the last N-2 columns of Q, then
$$
 Q’ \equiv R Q’.
$$
This can be shown by observing that any vectors orthogonal to the normal of a hyperplane are unaffected when reflected by that hyperplane. Since $R$ is generated with reflections on $u$ and/or $v$, $R$ cannot rotate vectors orthogonal to $u$ and $v$. 
Here's a link to a MATLAB implementation.