<p>You can decompose the matrix $\mathbf{M} = \mathbf{TRS}$ into basic transformations: translation, scaling, and rotation. Given this matrix:</p>

<p>$$\mathbf{M} = \begin{bmatrix}
a_{00} &amp; a_{01} &amp; a_{02} &amp; a_{03}\\
a_{10} &amp; a_{11} &amp; a_{12} &amp; a_{13}\\
a_{20} &amp; a_{21} &amp; a_{22} &amp; a_{23}\\
0 &amp; 0 &amp; 0 &amp; 1
\end{bmatrix}$$</p>

<p>You can decompose the translation by inspection using the last column $\mathbf{t} = (a_{03}, a_{13}, a_{23})$.</p>

<p>For scaling, we know that the first three columns of the matrix corresponds to the bases (axes). We can get the scale by the length/norm of these vectors, i.e. how much the bases were scaled. So the scale is $\mathbf{s} = (s_0, s_1, s_2)$ where:</p>

<p>$$\begin{matrix}
s_0 = \left \|(a_{00}, a_{10}, a_{20}) \right \|\\ 
s_1 = \left \|(a_{01}, a_{11}, a_{21}) \right \|\\ 
s_2 = \left \|(a_{02}, a_{12}, a_{22}) \right \|\\ 
\end{matrix}$$</p>

<p>Now you have the scale, you can get rid of it using the $3\times 3$ sub-matrix that corresponds to $\mathbf{RS}$ by multiplying the matrix with the inverse of the scale $\mathbf{S}^{-1}$ to get $\mathbf{R}$</p>

<p>$$\begin{align}
\mathbf{(RS)S}^{-1} &amp;= 
\begin{bmatrix}
a_{00} &amp; a_{01} &amp; a_{02}\\
a_{10} &amp; a_{11} &amp; a_{12}\\
a_{20} &amp; a_{21} &amp; a_{22}\\
\end{bmatrix}
\begin{bmatrix}
s_0 &amp; 0 &amp; 0\\ 
0 &amp; s_1 &amp; 0\\ 
0 &amp; 0 &amp; s_2
\end{bmatrix}^{-1} \\
&amp;=
\begin{bmatrix}
a_{00} &amp; a_{01} &amp; a_{02}\\
a_{10} &amp; a_{11} &amp; a_{12}\\
a_{20} &amp; a_{21} &amp; a_{22}\\
\end{bmatrix}
\begin{bmatrix}
1/s_0 &amp; 0 &amp; 0\\ 
0 &amp; 1/s_1 &amp; 0\\ 
0 &amp; 0 &amp; 1/s_2
\end{bmatrix}
\end{align}$$</p>

<p>Thus ($\mathbf{(RS)S}^{-1} = \mathbf{RI} = \mathbf{R}$):</p>

<p>$$\mathbf{R} =
\begin{bmatrix}
a_{00}/s_0 &amp; a_{01}/s_1 &amp; a_{02}/s_2\\
a_{10}/s_0 &amp; a_{11}/s_1 &amp; a_{12}/s_2\\
a_{20}/s_0 &amp; a_{21}/s_1 &amp; a_{22}/s_2\\
\end{bmatrix}$$</p>

<p>This is the final rotation matrix. You can further decompose it using many ways. It is quit lengthy but you can search for <em>decomposing a rotation matrix</em>.</p>

<hr>

<p>This method only gives an equivalent values in the form of translation, scaling and rotation (the original matrix maybe the result of other types of transformations). It may has problems with floating point precision with the rotation angles if you further used the decomposed angles, rounding errors may accumulate in the computations. You should not use it unless you did not construct the matrix yourself. </p>

<p>If you are the one who constructed the matrix and wanted the decomposition in order to be able to edit and display the translation, scale and rotation individually and <em>independently</em>, probabbly the cleanest why is to store the components of $\mathbf{t}$, $\mathbf{s}$ and $\mathbf{r}$ in a transform class individually as vectors (maybe quaternion for the rotation). Only when you need the transform matrix, construct a $\mathbf{TRS}$ matrix from these components (You can cache the matrix until some component is changed).</p>

CC BY-SA 3.0
2016-11-16T16:11:22.850Z
Can you clarify what the problems with floating-point precision are? I don't see anything in this method that would cause precision problems, unless the scale is really extreme. Also, worth noting that this method may fail if the matrix was composed from a sequence of matrices that includes both non-uniform scales and rotations. The $\mathbf{R}$ matrix will turn out not to be a rotation in that case, but will include some shear.
Nathan Reed
CC BY-SA 3.0
2016-11-16T19:14:17.193Z
All floating point numbers have intrinsic (bounded) error. Any time you perform operations, and particularly addition or subtraction, you compound the error, increasing the magnitude of the bounds.  Hidden in the decomposition algorithm are many addition operations (both in the matrix multiplication and the scale magnitude calculation) and a square root (in the scale).  Further decomposition will introduce further error.
Tim Sparkles
CC BY-SA 3.0
2016-11-16T20:31:34.843Z
@Timbo There isn't any full matrix multiplication here though, just multiplying the columns of the matrix by the inverse scales. And a vector magnitude involves adding all positive quantities, so there's no catastrophic cancellation there; it doesn't produce much relative error, AFAICT. Anyway, the author clarified that they're talking about further decomposing the rotation matrix into Euler angles or suchlike, which makes more sense.
Nathan Reed
CC BY-SA 3.0
2016-11-28T15:45:37.537Z
Thanks – great answer. Follow-up: to get the original matrix back, I am assuming we need to follow a certain order of operations, starting from the identity matrix. Would this order be TRS?
elsurudo
CC BY-SA 3.0
2016-11-16T16:02:41.793Z
2016-11-16T16:30:53.197Z
2016-11-16T16:30:53.197Z
user5488
<p>To be more concrete, I'm working on an iOS app, and have a <code>CATransform3D</code> struct (basically a 4x4 transform array).</p>

<p>Is it possible to deduce all the different "operations" this matrix implies? Things like how much rotation, scale, etc. it implies?</p>

<transformations>
Is it possible to turn a 3d rotation matrix (4x4) into its component parts (rotation, scale, etc.)?
elsurudo
# Is it possible to turn a 3d rotation matrix (4x4) into its component parts (rotation, scale, etc.)?

(Asked by: username_0 on 2016-11-16)

To be more concrete, I'm working on an iOS app, and have a `CATransform3D` struct (basically a 4x4 transform array).

Is it possible to deduce all the different "operations" this matrix implies? Things like how much rotation, scale, etc. it implies?

**Tags:** transformations


---

## 1 answers

### Answer by username_1 on 2016-11-16. Score: 11 | Accepted answer

You can decompose the matrix $\mathbf{M} = \mathbf{TRS}$ into basic transformations: translation, scaling, and rotation. Given this matrix:

$$\mathbf{M} = \begin{bmatrix}
a\_{00} & a\_{01} & a\_{02} & a\_{03}\\
a\_{10} & a\_{11} & a\_{12} & a\_{13}\\
a\_{20} & a\_{21} & a\_{22} & a\_{23}\\
0 & 0 & 0 & 1
\end{bmatrix}$$

You can decompose the translation by inspection using the last column $\mathbf{t} = (a\_{03}, a\_{13}, a\_{23})$.

For scaling, we know that the first three columns of the matrix corresponds to the bases (axes). We can get the scale by the length/norm of these vectors, i.e. how much the bases were scaled. So the scale is $\mathbf{s} = (s\_0, s\_1, s\_2)$ where:

$$\begin{matrix}
s\_0 = \left \|(a\_{00}, a\_{10}, a\_{20}) \right \|\\
s\_1 = \left \|(a\_{01}, a\_{11}, a\_{21}) \right \|\\
s\_2 = \left \|(a\_{02}, a\_{12}, a\_{22}) \right \|\\
\end{matrix}$$

Now you have the scale, you can get rid of it using the $3\times 3$ sub-matrix that corresponds to $\mathbf{RS}$ by multiplying the matrix with the inverse of the scale $\mathbf{S}^{-1}$ to get $\mathbf{R}$

$$\begin{align}
\mathbf{(RS)S}^{-1} &=
\begin{bmatrix}
a\_{00} & a\_{01} & a\_{02}\\
a\_{10} & a\_{11} & a\_{12}\\
a\_{20} & a\_{21} & a\_{22}\\
\end{bmatrix}
\begin{bmatrix}
s\_0 & 0 & 0\\
0 & s\_1 & 0\\
0 & 0 & s\_2
\end{bmatrix}^{-1} \\
&=
\begin{bmatrix}
a\_{00} & a\_{01} & a\_{02}\\
a\_{10} & a\_{11} & a\_{12}\\
a\_{20} & a\_{21} & a\_{22}\\
\end{bmatrix}
\begin{bmatrix}
1/s\_0 & 0 & 0\\
0 & 1/s\_1 & 0\\
0 & 0 & 1/s\_2
\end{bmatrix}
\end{align}$$

Thus ($\mathbf{(RS)S}^{-1} = \mathbf{RI} = \mathbf{R}$):

$$\mathbf{R} =
\begin{bmatrix}
a\_{00}/s\_0 & a\_{01}/s\_1 & a\_{02}/s\_2\\
a\_{10}/s\_0 & a\_{11}/s\_1 & a\_{12}/s\_2\\
a\_{20}/s\_0 & a\_{21}/s\_1 & a\_{22}/s\_2\\
\end{bmatrix}$$

This is the final rotation matrix. You can further decompose it using many ways. It is quit lengthy but you can search for *decomposing a rotation matrix*.

---

This method only gives an equivalent values in the form of translation, scaling and rotation (the original matrix maybe the result of other types of transformations). It may has problems with floating point precision with the rotation angles if you further used the decomposed angles, rounding errors may accumulate in the computations. You should not use it unless you did not construct the matrix yourself.

If you are the one who constructed the matrix and wanted the decomposition in order to be able to edit and display the translation, scale and rotation individually and *independently*, probabbly the cleanest why is to store the components of $\mathbf{t}$, $\mathbf{s}$ and $\mathbf{r}$ in a transform class individually as vectors (maybe quaternion for the rotation). Only when you need the transform matrix, construct a $\mathbf{TRS}$ matrix from these components (You can cache the matrix until some component is changed).

> **Comments:**
> **username_2**: Can you clarify what the problems with floating-point precision are? I don't see anything in this method that would cause precision problems, unless the scale is really extreme. Also, worth noting that this method may fail if the matrix was composed from a sequence of matrices that includes both non-uniform scales and rotations. The $\mathbf{R}$ matrix will turn out not to be a rotation in that case, but will include some shear.  
> **username_3**: All floating point numbers have intrinsic (bounded) error. Any time you perform operations, and particularly addition or subtraction, you compound the error, increasing the magnitude of the bounds.  Hidden in the decomposition algorithm are many addition operations (both in the matrix multiplication and the scale magnitude calculation) and a square root (in the scale).  Further decomposition will introduce further error.  
> **username_2**: @Timbo There isn't any full matrix multiplication here though, just multiplying the columns of the matrix by the inverse scales. And a vector magnitude involves adding all positive quantities, so there's no catastrophic cancellation there; it doesn't produce much relative error, AFAICT. Anyway, the author clarified that they're talking about further decomposing the rotation matrix into Euler angles or suchlike, which makes more sense.  
> **username_0**: Thanks – great answer. Follow-up: to get the original matrix back, I am assuming we need to follow a certain order of operations, starting from the identity matrix. Would this order be TRS?  

---

