Lecture 2 of 1417 min3D modelFree lecture

Matrices as transformations

A matrix is a machine that transforms vectors — stretching, rotating, shearing or flattening space. See matrices move grids in 2D and cubes in 3D, read the determinant as a change in area and volume, and understand matrix multiplication as a neural-network layer.

In this lecture you will

  • Read the columns of a matrix as the new positions of the basis vectors
  • Recognize stretches, rotations, shears, reflections and projections
  • Interpret the determinant as the change in area or volume
  • Multiply matrices and vectors, and explain why order matters

If vectors are the nouns of machine learning, matrices are the verbs. A matrix is a grid of numbers, but the useful way to think of one is as a transformation: feed it a vector and it returns a new vector. Every layer of a neural network, every rotation in computer graphics and every step of PCA is a matrix doing this.

Columns tell you everything

In two dimensions, every vector is a combination of the two basis vectors î = (1, 0) and ĵ = (0, 1). A matrix only needs to say where those two land — those are its columns — and every other vector follows along. The grid of the plane stays evenly spaced and parallel: the transformation is linear.

InteractiveA 2 × 2 matrix transforming the planeTry the presets, then drag the sliders
Loading visual…

The teal arrow is the first column (where î lands), the amber arrow the second (where ĵ lands). Watch the letter F: a reflection mirrors it, and the ‘flatten’ preset squashes everything onto a line.

To transform any vector, multiply: take x copies of the first column and y copies of the second.

(abcd)(xy)=x(ac)+y(bd)=(ax+bycx+dy)\begin{pmatrix} a & b \\ c & d \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} = x \begin{pmatrix} a \\ c \end{pmatrix} + y \begin{pmatrix} b \\ d \end{pmatrix} = \begin{pmatrix} ax + by \\ cx + dy \end{pmatrix}

The determinant

The determinant measures how much a transformation scales area (in 2D) or volume (in 3D). For a 2 × 2 matrix it is ad − bc. A determinant of 2 doubles areas; a negative one also flips space over, like a mirror; and a determinant of zero means space has been flattened into a lower dimension. Information is lost — many inputs land on the same output — so the transformation cannot be undone.

3D modelA 3 × 3 matrix transforming a cubeDrag to rotate
Loading 3D model…
  1. The identityA unit cube with a grid of points inside it. The three arrows are the basis vectors — the columns of the identity matrix.
  2. StretchA diagonal matrix stretches x by 1.8 and squashes z to 0.55. The volume changes by 1.8 × 1 × 0.55 = 0.99 — almost unchanged.
  3. ShearA shear slides layers of space sideways, like a deck of cards. The cube leans over, but its volume stays exactly the same: the determinant is 1.
  4. RotationA rotation turns space without stretching it. Lengths, angles and volume are all preserved.
  5. ProjectionSetting the middle row to zero sends every height to 0 and flattens the cube onto the floor. The determinant is 0: the heights are lost for good.
Matrix
A rectangular grid of numbers; as a transformation, its columns are the images of the basis vectors.
Linear transformation
A transformation that keeps grid lines straight, parallel and evenly spaced, with the origin fixed.
Determinant
The factor by which a square matrix scales areas or volumes; zero means the matrix cannot be inverted.
Inverse
The matrix that undoes a transformation. It exists only when the determinant is not zero.
Identity
The matrix that leaves every vector unchanged, with ones on the diagonal.

Matrix multiplication is a neural-network layer

Multiplying a matrix by a vector computes one dot product per row. That is precisely a dense neural-network layer: each row of the weight matrix holds one neuron’s weights, and each output is that neuron’s weighted sum, before the activation function.

AnimationHow a layer multiplies
Loading animation…
  1. Shapes firstA weight matrix with 3 rows and 4 columns meets an input with 4 numbers. The inner sizes match, so the output has 3 numbers — one per row.
  2. Row times columnThe first output is the dot product of the first row with the input: multiply pairs and add.
  3. Every row, same inputThe other rows do the same with their own weights. Each row picks out a different pattern in the input.
  4. A whole batchStack several inputs as columns and the same rule produces all the outputs at once. GPUs are built to do exactly this, very fast.
  5. Order mattersApplying A and then B is the product BA. Rotating then stretching gives a different shape from stretching then rotating: in general AB ≠ BA.
import numpy as np

W = np.array([[2, 0, -1, 1],
              [1, 3, 0, -2],
              [0, 1, 2, 1]])
x = np.array([1, 3, 2, -1])
print(W @ x)                        # [-1 12  6] — one dot product per row

X = np.array([[1, 0, 2], [3, 1, 0], [2, -1, 1], [-1, 2, 1]])
print(W @ X)                        # a batch of three inputs at once

Some vectors are special: a matrix only stretches them, without turning them. Those eigenvectors reveal the main directions of a transformation — and, applied to data, the main directions of variation. That is the next lecture.

Key takeaways

  • A matrix transforms space; its columns show where the basis vectors land, and every other vector follows.
  • The determinant is the factor by which areas or volumes change — negative when space is flipped, zero when it is flattened and information is lost.
  • Matrix × vector is a set of dot products, one per row — exactly what a neural-network layer computes before its activation.
  • Applying transformations one after another multiplies their matrices, and the order matters.

Check your understanding

4 questions · answers explained

Q1A 2 × 2 matrix has columns (2, 0) and (0, 3). What does it do?

The first column is where (1, 0) lands and the second where (0, 1) lands. Here they are simply stretched, and the determinant 2 × 3 = 6 is the area factor.

Q2A matrix has determinant 0. What follows?

Zero determinant means zero area (or volume) after the transformation — space collapses onto a line or plane. Different inputs land on the same output, so there is no inverse.

Q3A layer multiplies a 128 × 512 weight matrix by an input vector. How long must the input be, and how long is the output?

The inner sizes must match — (128 × 512) · (512 × 1) — and the result has one entry per row, 128.

Q4Why does the order of matrix multiplication matter?

Matrix products represent doing one transformation after another. In general AB ≠ BA, just as putting on socks then shoes differs from shoes then socks.