Rotation matrix for aligning x-axis in an arbitrary direction I want to align the x-axis of my coordinate system, with an arbitrary direction in space $\hat{n}$. About which axis should I rotate? Ceratinty rotation about x-axis or $\hat{n}$-axis will not serve the purpose. What would be the rotation matrix acting on my coordinate system that will serve my purpose?
 A: Let $\theta>0$ denote the angle between the $\hat{\mathbf x}$ and $\hat{\mathbf n}$.  Notice that 
$$
\hat{\mathbf u} = \frac{\hat{\mathbf x}\times\hat{\mathbf n}}{\sin\theta}
$$
is a unit vector perpendicular to both $\hat{\mathbf x}$ and $\hat{\mathbf n}$.  The desired rotation is a right-handed rotation around $\hat{\mathbf u}$ by the angle $\theta$.
A: There is a very quick and clean way of doing this, which is presented in

Building an Orthonormal Basis from a 3D Unit Vector Without Normalization. JR Frisvad. J. Graphics Tools 16 no. 3, 151 (2012).

Suppose you have a normalized vector $\vec n=(n_x,n_y,n_z)^T$, and you want a rotation matrix that will take the $z$ axis into $\vec n$. (Here it's important to note that this will never be unique, as a further rotation about $\vec n$ is always possible, but the question sort of assumes that you don't care about this degree of freedom.)
As Josh mentions, this is accomplished by doing a rotation by $\theta=\arccos(\vec n\cdot\hat z)$ about the axis $\hat u=\vec n\times\hat z/\sin(\theta)$, and this can be used to build up a rotation matrix using these techniques from Wikipedia but that's a lot of work.
Alternatively, you can find two other vectors orthogonal to $\vec n$ and stick them on a matrix, by choosing e.g. the $\hat x$ unit vector and taking $\hat n\times\hat x$ as your initial vector, but normalizing them is generally a huge pain.
Instead, Frisvad gets, via quaternionic methods, a simple triplet of orthogonal vectors,
$$
\begin{pmatrix}1-\frac{n_x^2}{1+n_z} \\ -\frac{n_x n_y}{1+n_z} \\ -n_x \end{pmatrix}
,\ 
\begin{pmatrix}-\frac{n_x n_y}{1+n_z} \\ 1-\frac{n_y^2}{1+n_z} \\ -n_y \end{pmatrix}
\ \text{and} \ 
\begin{pmatrix}n_x \\ n_y \\ n_z\end{pmatrix}
$$
and these give you your matrix directly,
$$
\begin{pmatrix}
1-\frac{n_x^2}{1+n_z}  & -\frac{n_x n_y}{1+n_z} & n_x \\
-\frac{n_x n_y}{1+n_z} & 1-\frac{n_y^2}{1+n_z}  & n_y \\
          -n_x         &           -n_y         & n_z
\end{pmatrix}.
$$
This can be verified directly to be an orthogonal matrix, and it satisfies the criterion, so you're essentially set. In addition, this is simple both conceptually and numerically, and it only faces numerical trouble when $n_z$ is close to $-1$, in which case $n_x$ and $n_y$ are quadratically closer to zero than $1+n_z$, so even then it should be quite stable numerically.
A: In 3 dimensions you can use the cross product to get an appropriate rotation axis.
If $\hat{x}$ and $\hat{n}$ are non-parallel 3-dimensional unit vectors then $\vec{s}=\hat{n}\times \hat{x}$ is non-zero. Since $\vec{s}$ is orthogonal to both $\hat{n}$ and $\hat{x}$ there is some rotation about $\vec{s}$ that takes $\hat{n}$ to $\hat{x}$. You can get the requires angle of rotation with the dot product $\hat{n}\cdot \hat{x}=\cos \theta$.
Caveats: 1)If $\hat{x}$ is parallel or anti-parallel to $\hat{n}$ then $\vec{s} = 0$ and this  technique will fail (but then it is obvious how to make $\hat{n}=\hat{x}$). 2) Remember that $\vec{s}$ is not necessarily a unit vector, which might affect the construction of your rotation matrix. 3) This technique might not be great numerically, though I bet you can use some vector identities to make it more stable.
