How to find matrix which rotates one unit vector into another in 3D space?

I have the unit vector $\begin{bmatrix} x \\ y \\ z \end{bmatrix}$ and I want to rotate a linear transformation which rotates it into the vector $\begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}$. A simple matrix which I found to do the vector converting part is:

$$ \begin{bmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}$$

but, the issue is that the above collapses whole of space into the $z$ line, I want the matrix which rotates the vector into the required without collapsing space.


Let $v$ be your vector. If $x=y=0$, then, since it is an unit vector, $z=\pm1$. If $z=1$, you can take the identity, and if $z=-1$, you can take a half-turn around any vector orthogonal to $(0,0,1)$.

Now, I will assume that $(x,y)\ne(0,0)$. Let$$w_1=\left(\frac y{\sqrt{x^2+y^2}},\frac{-x}{\sqrt{x^2+y^2}},0\right),$$which is an unit vector which is orthogonal to both $v$ and $(0,0,1)$, and let$$w_2=\left(\frac x{\sqrt{x^2+y^2}},\frac y{\sqrt{x^2+y^2}},0\right),$$which is also an unit vector and which is orthogonal to both $w_1$ and $(0,0,1)$. So, $\bigl\{w_1,w_2,(0,0,1)\bigr\}$ is an orthonormal basis of $\Bbb R^3$. Take $\theta\in[0,\pi]$ such that $\cos(\theta)=z$ and that $\sin(\theta)\geqslant0$ (which implies that $\sin(\theta)=\sqrt{1-z^2}=\sqrt{x^2+y^2}$) and note that, since $\cos\theta=z=\bigl\langle(v,(0,0,1)\bigr\rangle$, $\theta$ is the angle between $v$ and $(0,0,1)$. Consider the rotation $R$ around $w_1$ with angle $\theta$. More precisely, consider the linear map $R\colon\Bbb R^3\longrightarrow\Bbb R^3$ such that:

  • $R(w_1)=w_1$;
  • $R(w_2)=\cos(\theta)w_2-\sin(\theta)(0,0,1)$;
  • $R(0,0,1)=\sin(\theta)w_2+\cos(\theta)(0,0,1)=v$.

Then $R$ is a rotation that maps $(0,0,1)$ into $v$. The matrix of $R$ with respect to the standard basis of $\Bbb R^3$ is$$\begin{bmatrix}z+\frac{y^2}{z+1} & -\frac{x y}{z+1} & x \\ -\frac{x y}{z+1} & z+\frac{x^2}{z+1} & y \\ -x & -y & z\end{bmatrix}.$$What you're after is $R^{-1}$, which is the transpose of $R$, since $R$ is an orthogonal matrix. So, an answer to your question is$$\begin{bmatrix}z+\frac{y^2}{z+1}&-\frac{x y}{z+1}&-x\\-\frac{x y}{z+1}&z+\frac{x^2}{z+1}&-y\\x&y&z\end{bmatrix}.$$


There is an infinite number of possible rotation matrices, but there is one rotation matrix that has the minimum rotation angle, and that is generated as follows.

Let $u= \begin{bmatrix} x \\ y \\ z \end{bmatrix} $ and $ v = \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} $

Define the unit vector $w$ as the normalized cross product as follows

$ w = \dfrac{u \times v} { \| u \times v \| } $

Vector $w$ is the rotation axis. The rotation angle $\theta = \cos^{-1} u \cdot v $

Now the rotation matrix can be expressed using the Rodrigues' rotation matrix formula:

$R = w w^T + (I - w w ^T ) \cos \theta + S_w \sin \theta $

where

$S_w = \begin{bmatrix} 0 && - w_z && w_y \\ w_z && 0 && -w_x \\ -w_y && w_x && 0 \end{bmatrix}$

On the other hand, to obtain the rotation matrix with the maximum rotation angle of $180^\circ$, just use, as the rotation axis, the vector

$ w = \dfrac{ u + v }{\| u + v \| } $

And apply the Rodrigues' formula but with $\theta = 180^\circ$, which simplifies it into,

$ R = w w^T + (I - w w ^T ) (-1) + 0 = 2 w w^T - I $