3D rotation matrix around vector I am using matrix for performing 3D rotations. I know that in 3D space the matrix product order is important - changing the order of the matrices can effect the rotate result.

So I am interesting about how can I create a rotate matrix that perform rotation (clockwise) around some vector, say $(1, 0, 1)$.

I thought that $R_{(1,0,1)}(\theta) = R_X(\theta) \cdot R_Z(\theta)$ but it seems wrong because if I tries to rotate the $(1, 0, 1)$ with $45^{\circ}$ it moves out from $(1, 0, 1)$ but it should stay in place.

So how can I build such a rotation matrix as product of 3D trasformation matrices?
There is a direct formula for the rotation matrix for an arbitrary axis and angle. Given a unit vector $a = (a_x, a_y, a_z)$ and angle $\theta$, the matrix can be constructed as follows (derivation from Wikipedia):

First build a matrix $C$ from the components of $a$ according to the following formula:

$$ C =
\begin{bmatrix}
  0 &amp; -a_z &amp; a_y \\
  a_z &amp; 0 &amp; -a_x \\
  -a_y &amp; a_x &amp; 0
\end{bmatrix} $$

Then you can construct the rotation matrix from $C$ as follows:

$$ R_a(\theta) = I + C \sin \theta + C^2 (1 - \cos \theta) $$

where $I$ is the 3&times;3 identity matrix. This assumes you're using a column-vector convention; if you're using row vectors instead, transpose $C$.

By the way, this formula has a neat geometric interpretation. The matrix $C$ has the effect of calculating cross products with $a$. In other words, for any vector $v$, $Cv = a \times v$. If you imagine rotating some vector $v$ about $a$, the tip of $v$ traces out a circle perpendicular to $a$; $Cv$ is a vector tangent to that circle, and $C^2 v$ (which equals $a \times (a \times v)$) is a vector normal to the circle. So with those two vectors, you have a basis for the plane of the circle; then you can apply a variation of the usual sine/cosine formula for points on a circle.