Ma · Mathematics

How to Combine Transformation Matrices

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose a convention

    Use column vectors and homogeneous coordinates, or state clearly if your convention differs.

  2. 2. Write each matrix

    Represent every rotation, scaling, reflection, shear, or translation as a compatible matrix.

  3. 3. Multiply in order

    For column vectors, the transformation applied first appears on the right.

  4. 4. Check with a point

    Apply the matrices individually to one point and compare with the combined matrix.

Try it: Matrix calculator

Transformation matrices are combined by composition: apply one transformation to a point, then apply the next transformation to the result. The main issue is the order of multiplication.

The convention used here is the standard computer-graphics and linear-algebra convention of column vectors. Under this convention, if AA is applied first and BB second, the combined transformation is BABA, not ABAB.

The basic rule

Let a point or vector be represented by a column vector x\mathbf{x}. If the first transformation is represented by AA and the second by BB, then

x′=Ax\mathbf{x}' = A\mathbf{x}

and then

x′′=Bx′=B(Ax)=(BA)x.\mathbf{x}'' = B\mathbf{x}' = B(A\mathbf{x}) = (BA)\mathbf{x}.

Therefore, the combined matrix is

M=BA.M = BA.

Read the product from right to left: the rightmost matrix acts first.

For several transformations, the same principle gives

M=AnAn−1⋯A2A1,M = A_n A_{n-1}\cdots A_2 A_1,

where A1A_1 is applied first and AnA_n is applied last.

Combining linear transformations

In two dimensions, a linear transformation has the form

[x′y′]=A[xy],\begin{bmatrix}x'\\y'\end{bmatrix} = A\begin{bmatrix}x\\y\end{bmatrix},

where AA is a 2×22\times 2 matrix. For example, a counterclockwise rotation by angle θ\theta is

R(θ)=[cos⁡θ−sin⁡θsin⁡θcos⁡θ].R(\theta)= \begin{bmatrix} \cos\theta & -\sin\theta\\ \sin\theta & \cos\theta \end{bmatrix}.

Scaling by factors sxs_x and sys_y is represented by

S=[sx00sy].S= \begin{bmatrix} s_x & 0\\ 0 & s_y \end{bmatrix}.

If you rotate first and scale second, the combined matrix is

M=SR(θ).M=SR(\theta).

If you scale first and rotate second, it is instead

M=R(θ)S.M=R(\theta)S.

These products are generally different because matrix multiplication is not commutative:

SR(θ)≠R(θ)S.SR(\theta)\ne R(\theta)S.

Including translations with homogeneous coordinates

A translation cannot be represented by an ordinary 2×22\times 2 matrix acting on (x,y)(x,y), because translation is not a linear transformation. Introduce a third coordinate and write the point as

p=[xy1].\mathbf{p}=\begin{bmatrix}x\\y\\1\end{bmatrix}.

A translation by (tx,ty)(t_x,t_y) is then represented by the 3×33\times 3 matrix

T=[10tx01ty001].T= \begin{bmatrix} 1&0&t_x\\ 0&1&t_y\\ 0&0&1 \end{bmatrix}.

For example, the homogeneous matrices for scaling and rotation are

S=[sx000sy0001],R(θ)=[cos⁡θ−sin⁡θ0sin⁡θcos⁡θ0001].S= \begin{bmatrix} s_x&0&0\\ 0&s_y&0\\ 0&0&1 \end{bmatrix}, \qquad R(\theta)= \begin{bmatrix} \cos\theta&-\sin\theta&0\\ \sin\theta&\cos\theta&0\\ 0&0&1 \end{bmatrix}.

This lets you combine scaling, rotation, reflection, shear, and translation in one product.

Fully worked example

Suppose a point is transformed in this order:

  1. Rotate it counterclockwise by 90∘90^\circ.
  2. Scale the xx-coordinate by 22 and the yy-coordinate by 33.
  3. Translate it by (1,−2)(1,-2).

Using homogeneous coordinates, the matrices are

R=[0−10100001],S=[200030001],T=[10101−2001].R= \begin{bmatrix} 0&-1&0\\ 1&0&0\\ 0&0&1 \end{bmatrix}, \qquad S= \begin{bmatrix} 2&0&0\\ 0&3&0\\ 0&0&1 \end{bmatrix}, \qquad T= \begin{bmatrix} 1&0&1\\ 0&1&-2\\ 0&0&1 \end{bmatrix}.

Because the order is rotation, then scaling, then translation, the combined matrix is

M=TSR.M=TSR.

First multiply SS and RR:

SR=[0−20300001].SR= \begin{bmatrix} 0&-2&0\\ 3&0&0\\ 0&0&1 \end{bmatrix}.

Now multiply by TT:

M=TSR=[0−2130−2001].M=TSR= \begin{bmatrix} 0&-2&1\\ 3&0&-2\\ 0&0&1 \end{bmatrix}.

Thus every point can be transformed in one step using

[x′y′1]=[0−2130−2001][xy1].\begin{bmatrix} x'\\y'\\1 \end{bmatrix} = \begin{bmatrix} 0&-2&1\\ 3&0&-2\\ 0&0&1 \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix}.

For the point (1,2)(1,2),

M[121]=[−311].M\begin{bmatrix}1\\2\\1\end{bmatrix} = \begin{bmatrix}-3\\1\\1\end{bmatrix}.

Checking step by step gives the same result: rotation sends (1,2)(1,2) to (−2,1)(-2,1), scaling sends this to (−4,3)(-4,3), and translation sends it to (−3,1)(-3,1).

Common mistakes

Reversing the order

For column vectors, the first operation is the rightmost factor. If rotation comes first and translation comes second, use TRTR, not RTRT.

Multiplying entries element by element

Combining matrices means ordinary matrix multiplication. It does not mean multiplying corresponding entries. Each entry of a product is obtained from a row-column dot product.

Treating translations as 2×22\times 2 matrices

A translation requires homogeneous coordinates, usually a 3×33\times 3 matrix in two dimensions and a 4×44\times 4 matrix in three dimensions.

Mixing row and column conventions

Some applications use row vectors, written as xTA\mathbf{x}^{\mathsf{T}}A. In that convention, the multiplication order is reversed: if AA is applied first and BB second, the combined matrix is ABAB. Neither convention is wrong, but the vector orientation and order must remain consistent.

Ignoring coordinate systems

A transformation may be described in world coordinates, object coordinates, or coordinates relative to a pivot. Transforming about a pivot often requires several matrices. For example, to rotate about a point c\mathbf{c}, translate by −c-\mathbf{c}, rotate, then translate by c\mathbf{c}:

M=TcRT−c.M=T_{\mathbf{c}}RT_{-\mathbf{c}}.

Again, the rightmost operation happens first.

A practical verification method

After forming the product, test it on a simple point such as (1,0)(1,0) or (0,0)(0,0). Compute the result both ways: apply each transformation separately, and apply the combined matrix once. If the answers differ, check the multiplication order, vector convention, and matrix dimensions.

References

  1. Introduction to Linear Algebra — Gilbert Strang

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.