# Expression of rotation matrix from two vectors

What is the matrix expression of the rotation matrix in 3D which turns a vector $\vec{a}$ into a vector $\vec{b}$, with both vectors given by their coordinates? ($\vec{a} = (a_x, a_y, a_z)$ and $\vec{b} = (b_x, b_y, b_z)$, both already normalized).

Other answers give a construction using an augmented 3D rotation matrix, where the angle and the base change matrices are given using the dot/cross products, but I couldn't find a direct expression of the 9 matrix fields using the 6 vector coordinates.

• There is usually more than one possible rotation that takes vector $a$ to vector $b$. – David K Apr 22 '15 at 14:26
• In fact, that is always the case in dimension $\geq 3$, as we can always compose a rotation that does this with a rotation that fixes $b$ (and there are infinitely many of these in such dimensions). – Travis Apr 22 '15 at 14:30
• But in 3D I guess there is only one free dimension, so one variable that can take any value, corresponding to an additional rotation rolling around the aligned vector. – tmlen Apr 22 '15 at 14:32
• That's right---but at least when $\bf a$ and $\bf b$ are linearly independent, one can get a preferred rotation by insisting that it fix ${\bf a} \times {\bf b}$, i.e., by rotating $\bf a$ and $\bf b$ in the plane they span. – Travis Apr 22 '15 at 14:34

## 1 Answer

As David K mentions, there are infinitely many rotations that map $\bf a$ to $\bf b$---in fact, there are precisely a circle's worth of such rotations.

If the vectors $\bf a$ and $\bf b$ are linearly independent, however, then we can pick out a preferred rotation, namely the one that fixes ${\bf a} \times {\bf b}$ (equivalently, the unique rotation that preserves the plane spanned by $\bf a$ and $\bf b$ as well as the orientation of that plane).

Here's one way to construct the matrix corresponding to this preferred rotation:

The vector $${\bf n} := \frac{{\bf a} \times {\bf b}}{|{\bf a} \times {\bf b}|}$$ has unit length and is orthogonal to $\bf a$, and hence ${\bf a}$ and ${\bf b}$ together determine an oriented, orthonormal basis of $\Bbb R^3$, namely, $$({\bf a}, {\bf n} \times {\bf a}, {\bf n}).$$ In particular, the matrix $$\begin{pmatrix}{\bf a} & {\bf n} \times {\bf a} & {\bf n}\end{pmatrix}$$ defines a rotation, namely the one that sends the standard basis $({\bf e}_1, {\bf e}_2, {\bf e}_3)$ to the above basis, and its inverse does the reverse.

Now, by symmetry, $$({\bf b}, {\bf n} \times {\bf b}, {\bf n}).$$ is also an oriented, orthonormal basis, and the corresponding matrix built by adjoining these (column) vectors sends the standard basis to this one.

Putting this together with the inverse mentioned above maps $({\bf a}, {\bf n} \times {\bf a}, {\bf n})$ to the standard basis $({\bf e}_1, {\bf e}_2, {\bf e}_3)$ and then the standard basis to $({\bf b}, {\bf n} \times {\bf b}, {\bf n})$, that is, by construction $$\begin{pmatrix}{\bf b} & {\bf n} \times {\bf b} & {\bf n}\end{pmatrix}\begin{pmatrix}{\bf a} & {\bf n} \times {\bf a} & {\bf n}\end{pmatrix}^{-1}$$ is a rotation matrix is maps $\bf a$ to $\bf b$ and fixes $\bf n$ (and hence ${\bf a} \times {\bf b}$). (Note that since the inverted matrix $\begin{pmatrix}{\bf a} & {\bf n} \times {\bf a} & {\bf n}\end{pmatrix}$ is orthogonal, its inverse is just its transpose.)

One can of course expand this to find explicit formulas for the entries of the resulting matrix in terms of the components of $\bf a$ and $\bf b$, but I don't doubt that this would simplify nicely.