A Quick Trick for Finding a Matrix Transformation Formula

by Justin Skycak on

Perform the desired transformation on identity matrix to get a left-multiplier, and maybe transpose the output.

Here’s a trick that you can (often) use to find a desired matrix transformation formula: just find the output of the transformation for the identity matrix.

For instance, if you want to rotate 90 degrees clockwise, then you want to take the identity matrix

$$\begin{align*} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \end{align*}$$

and turn it into the following:

$$\begin{align*} \begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{bmatrix} \end{align*}$$

Now, sometimes you have to tweak the result by transposing it afterwards – we don’t catch this in the above reasoning since the identity matrix is symmetric.

But you can catch that by double-checking that your transformation works in a more general case as follows:

$$\begin{align*} &\begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{bmatrix} \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \\ &= \begin{bmatrix} 7 & 8 & 9 \\ 4 & 5 & 6 \\ 1 & 2 & 3 \end{bmatrix} \\ & \textrm{transpose the result} \\ & \begin{bmatrix} 7 & 4 & 1 \\ 8 & 5 & 2 \\ 9 & 6 & 3 \end{bmatrix} \end{align*}$$

So, if you want to rotate a matrix 90 degrees clockwise then you use rotate90(X) = transpose(antiDiag * X) where antiDiag is the antidiagonal matrix with 1’s on the anti-diagonal.