Skip to content

Matrix Multiplication and Transformations

Matrix multiplication is the most important operation in linear algebra. In this lesson you’ll learn how it works and see how matrices represent transformations like rotation, scaling, and shearing in 2D and 3D.

To multiply AA (size m×nm \times n) by BB (size n×pn \times p), the number of columns in AA must equal the number of rows in BB. The result is an m×pm \times p matrix.

Each entry in the result is a dot product: row ii of AA dotted with column jj of BB.

(AB)ij=k=1naikbkj(AB)_{ij} = \sum_{k=1}^{n} a_{ik} \, b_{kj}
A B AB 1 2 3 4 × 5 6 7 8 = 19 22 43 50 Row 1 of A Row 2 of A Col 1 of B Col 2 of B row 1 · col 1 = 1·5 + 2·7 = 19 row 1 · col 2 = 1·6 + 2·8 = 22 row 2 · col 1 = 3·5 + 4·7 = 43 row 2 · col 2 = 3·6 + 4·8 = 50

In the diagram: each entry of the result matrix is computed by taking a row from the left matrix and a column from the right matrix, multiplying corresponding entries, and summing. The color-coded highlights show which row and column produce each result entry.

Key properties:

  • Matrix multiplication is NOT commutative: ABBAAB \neq BA in general
  • It IS associative: (AB)C=A(BC)(AB)C = A(BC)
  • The identity matrix II satisfies AI=IA=AAI = IA = A

Matrices represent linear transformations, operations that take vectors as input and produce new vectors as output.

Common transformations:

Rotation by angle θ\theta (2D):

R=[cosθsinθsinθcosθ]R = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}

Scaling by sxs_x and sys_y:

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

Reflection across the x-axis:

[1001]\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}
x y -2 -1 1 2 -2 -1 1 2 45° (1,1) (1,0) (-0.71, 0.71) (0, 1.41) Original unit square After 45° rotation

In the diagram: a unit square (blue) is transformed by a 45° rotation matrix. The green shape shows the result after applying the transformation. Each vertex gets multiplied by the matrix to produce its new position.

In games, these are combined into a single model matrix that transforms every vertex of a 3D model.

Example 1: 2x2 Matrix Multiplication

[1234][5678]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}

Top-left: (1)(5)+(2)(7)=5+14=19(1)(5) + (2)(7) = 5 + 14 = 19

Top-right: (1)(6)+(2)(8)=6+16=22(1)(6) + (2)(8) = 6 + 16 = 22

Bottom-left: (3)(5)+(4)(7)=15+28=43(3)(5) + (4)(7) = 15 + 28 = 43

Bottom-right: (3)(6)+(4)(8)=18+32=50(3)(6) + (4)(8) = 18 + 32 = 50

=[19224350]= \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}

Example 2: Rotation (90° counterclockwise)

The 90° rotation matrix is:

R=[0110]R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}

Apply it to the point (1,0)(1, 0):

[0110][10]=[01]\begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \end{bmatrix}

The point moves from (1,0)(1, 0) to (0,1)(0, 1), a quarter turn counterclockwise.

Example 3: Dimension Check

AA is 2×32 \times 3, BB is 3×43 \times 4. Can we multiply?

Inner dimensions match (3=33 = 3), so yes. Result is 2×42 \times 4.

Can we compute BABA? BB is 3×43 \times 4, AA is 2×32 \times 3. Inner dimensions are 424 \neq 2, so no. Order matters.

Matrix multiplication powers almost everything you see on screen in modern games:

  • Every frame, the engine multiplies the model matrix ×\times view matrix ×\times projection matrix to transform vertices from 3D world space to 2D screen space
  • Character animations use skeletal animation with bone matrices multiplied together
  • Camera movement is handled by updating the view matrix
  • GPUs are literally designed to do matrix multiplication as fast as possible

Example: When you rotate your camera in a first-person game, the engine updates the view matrix and multiplies it with every object’s model matrix. This happens for every vertex, every frame, thousands of times per second.

To multiply matrix $A$ ($2 \times 3$) by matrix $B$ ($3 \times 5$), the result has size:
A rotation matrix is an example of:
In game engines, the final vertex position on screen is usually calculated by multiplying:
Matrix multiplication is:
Rotating the point $(1, 0)$ by $90°$ counterclockwise gives: