The rotation matrix that will convert from local coordinates to global coordinates, for any element, is constructed from a basic 3x3 rotation matrix, developed as follows.

Given:

$\vec{X}=$ Unit vector of the global X-axis

$\vec{Y}=$ Unit vector of the global Y-axis

$\vec{Z}=$ Unit vector of the global Z-axis

$\vec{x}=$ Unit vector of the local x-axis

$\vec{y}=$ Unit vector of the local y-axis

$\vec{z}=$ Unit vector of the local z-axis

The basic 3x3 rotation matrix to convert from local to global coordinates is computed as:

$[R]=\begin{bmatrix} \vec{X} \cdot \vec{x} & \vec{X} \cdot \vec{y} & \vec{X} \cdot \vec{z} \\ \vec{Y} \cdot \vec{x} & \vec{Y} \cdot \vec{y} & \vec{Y} \cdot \vec{z} \\ \vec{Z} \cdot \vec{x} & \vec{Z} \cdot \vec{y} & \vec{Z} \cdot \vec{z}\end{bmatrix}$

This basic 3x3 rotation matrix can be used to rotate a single point, in vector format, from local to global coordinates.

$\vec{p}=\begin{bmatrix} x \\ y \\ z \end{bmatrix}=$ Point in local coordinates

$\vec{P}=[R] \vec{p}=$ Point in global coordinates

Alternately, a point can be rotated from global to local coordinates using the transpose of the basic rotation matrix:

$\vec{p}=[R]^T \vec{P}$

The full rotation matrix for an element to convert from local to global coordinates is constructed as an assembly of multiple basic rotation matrices:

$[R_{full}]=\begin{bmatrix} [R]{3x3} & [0]{3x3} & [0]{3x3} & [0]{3x3} \\ [0]{3x3} & [R]{3x3} & [0]{3x3} & [0]{3x3} \\ [0]{3x3} & [0]{3x3} & [R]{3x3} & [0]{3x3} \\ [0]{3x3} & [0]{3x3} & [0]{3x3} & [R]{3x3} \end{bmatrix}$

The above example would be the rotation matrix for a Timoshenko beam element, which has six degrees-of-freedom at each node, for a total of 12 degrees-of-freedom. As more ore less nodes and/or degrees-of-freedom are involved, the size of the full rotation matrix will adjust.