Decomposition of 4x4 or larger affine transformation matrix to individual variables per degree of freedom There are a couple of problems and solutions where affine matrices are decomposed into their separate transformations. However, they are all for the 2D case and I`m finding it difficult to generalise it to higher dimensions. Specifically, I have to decompose them for the 3D case, to variables that represent the individual degrees of freedom such as rotations, translations, scalings, and shears. I don't understand how to do that. Should there be a general approach for the nD case? Should I write out the formulas?
So I think I could write my problem for 3D like this, where I know all entries of A. With unknowns $v$ the scaling vector, $s$ the information needed for shearing which I don't know how to represent, $p$ the translation vector and $\theta, \psi, \phi$ the rotations: $$ A(\theta, \psi, \phi, v, s, p) = R_z(\phi) R_y(\psi) R_x(\theta) Sc(v) Sh(s) + T(p) $$
Maybe I should formulate this differently? 
User7530 shows a nice decomposition here but does not explain (enough for my understanding) why and how it works. Especially why the QR decomposition does the trick here and why $\theta = \arctan\left(\frac{d}{a}\right).$
John does domething similar but with less linear algebra and more algebra.
Michael Albanese shows a nice group theory approach to explain what an affine matrix is made up of, but does not say anything about decomposition. Also I don't really understand this.
 A: Factoring out the translation, assume that that the remaining $N$ by $N$ matrix is $A=QKS$ where $Q$ is a rotation matrix, $K$ is a scaling matrix and $S$ is a shear matrix. Then $A$ can be decomposed using QR decomposition (say by using Gram–Schmidt) into a rotation matrix $Q$ and an upper triangular matrix $R$ such that $A=QR$.
Next, decompose $R$ by setting the scaling matrix $K$ to the diagonal elements of $R$ and then computing $S=K^{-1}R$. Note that the inverse of $K$ can be computed by just taking the reciprocal of its diagonal elements.
Now the determinant of $Q$ is $1$, thus it is a rotation matrix. $K$ is a scaling matrix since all elements off the main diagonal are zero. $S$ is a shear matrix since all main diagonal elements are $1$ and its rank and trace are equal to $N$, its determinant is $1$ and all its eigenvalues are equal to $1$.
I believe this factorization works for any square matrix $A$ that has a non-zero determinant. For small $N$, it may be practical to find algebraic solutions for $Q$, $K$ and $S$. See this for computing Euler angles from $Q$.
