Matrices as transformations
A matrix stretches, rotates and projects space.
Create a free account Try part of it here
Why it matters
So far a matrix has been a table of data. There's a second way to see one: as a machine that moves every point on a page. It can stretch the page, turn it, slant it or squash it. That's what each layer of a neural network does to its inputs. It bends the space until things that should be told apart (spam and not spam, one topic and another) sit in different places, easy to separate. Seeing matrices as motions makes deep learning feel like geometry instead of bookkeeping.
What you'll be able to do
- See a matrix as a function that moves every point of space
- Read a 2×2 matrix's columns as where the basis vectors land
- Recognize scaling, rotation, shear, reflection and projection matrices
- Use the determinant to tell how much a transformation stretches, flips or flattens space
What it covers
- A matrix moves points
- The trick: watch two arrows
- A small zoo of motions
- The determinant: how much does area change?
- Worked example: three determinants by hand
- One motion after another
- Columns, not rows
A matrix moves points
Take a sheet of graph paper with a dot on it. Now stretch the paper sideways to twice its width. Every dot moves: one that was at [1, 2] is now at [2, 2]. A rule like "double the first number" is a transformation: something that takes any point and moves it somewhere new.
Multiplying a point by a matrix does exactly this. Feed in a vector, get out a moved vector. So a matrix is a little machine: point in, new point out.
The transformations a matrix can make are called linear, and they follow three rules:
- the center point
[0, 0]stays put; - straight lines stay straight;
- evenly spaced grid lines stay evenly spaced (though they can tilt and stretch).
So a matrix can stretch, squash, turn, slant or flip the page, but never bend or tear it.
In plain words: multiplying by a matrix moves every point in a regular way: stretching, turning, slanting or flipping the whole page at once.
The trick: watch two arrows
To understand what a 2 × 2 matrix does, you only need to watch two arrows: one step right, [1, 0], and one step up, [0, 1]. Wherever those two land, everything else follows, because every point is "some steps right plus some steps up".
And here's the shortcut: the two columns of the matrix are exactly where those two arrows land. Take this matrix:
[[2, 1], [0, 1]]
Its first column is [2, 0]: the "one step right" arrow is stretched to two steps right. Its second column is [1, 1]: the "one step up" arrow now points up and to the right.
So where does [3, 2] land? It's 3 steps right plus 2 steps up. Each step right becomes [2, 0] and each step up becomes [1, 1]:
3 × [2, 0] + 2 × [1, 1] = [6, 0] + [2, 2] = [8, 2]
In plain words: a matrix's columns tell you where "one step right" and "one step up" land. Every other point follows from those two.
Go deeper The notation
The two basic arrows are called ("i-hat", one step right) and ("j-hat", one step up):
Read it as: "A sends i-hat to its first column and j-hat to its second column."
Try each preset. Watch where î (green) and ĵ (blue) land: they're the matrix's columns. The blend slider shows the motion from no change at all.
Show the answer
Makes everything 3 times bigger in both directions
One step right lands on [3, 0] and one step up on [0, 3]: both stretched to three times their length.
A small zoo of motions
| What it does | Columns (where right and up land) |
|---|---|
| stretch sideways by 2, squash up-down by half | [2, 0] and [0, 0.5] |
| turn a quarter-turn anticlockwise | [0, 1] and [−1, 0] |
| slant (shear): slide the top sideways | [1, 0] and [1, 1] |
| mirror left-right | [−1, 0] and [0, 1] |
| flatten the whole page onto one line | [1, 0.5] and [2, 1] |
Here's a square's four corners being turned a quarter-turn in Python:
import numpy as np theta = np.radians(90) R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) square = np.array([[0, 0], [1, 0], [1, 1], [0, 1]]) # corners, one per row print((square @ R.T).round(2)) # each corner rotated 90°
The corners are stored one per row, so the code writes square @ R.T. The .T flips the matrix (you'll meet it in the next lesson) so it applies to every row at once.
The determinant: how much does area change?
Draw a 1 × 1 square on the page, then apply the matrix. The square becomes a slanted box (a parallelogram). The determinant is the number that says how its area changed.
For a 2 × 2 matrix with numbers a, b on the top row and c, d on the bottom, it's:
determinant = a × d − b × c
What the answer means:
- 2: areas double;
- 1: areas stay the same (turns and slants do this);
- negative: the page was flipped over, like a mirror;
- 0: the page was flattened onto a line. Two different points can land on the same spot, so you can't undo it. Information was lost.
In plain words: the determinant is how much the matrix grows or shrinks areas. Zero means it squashed the page flat, and that can't be reversed.
Go deeper The formula
Read it as: "the determinant is the top-left times the bottom-right, minus the top-right times the bottom-left."
a, b, c, d = 2, 0, 0, 3 # the matrix [[a, b], [c, d]] print(a * d - b * c)
Show the answer
6
The unit square becomes a 2 by 3 rectangle, so every area is multiplied by 6.
Worked example: three determinants by hand
Work out the determinant of each matrix by hand (a × d − b × c), then check with NumPy. What does each one do to areas?
import numpy as np stretch = np.array([[2, 0], [0, 3]]) mirror = np.array([[-1, 0], [0, 1]]) flatten = np.array([[1, 2], [0.5, 1]]) for name, M in [("stretch", stretch), ("mirror", mirror), ("flatten", flatten)]: print(name, round(float(np.linalg.det(M)), 3))
Stretch: 2 × 3 − 0 × 0 = 6, so areas grow six times. Mirror: −1 × 1 − 0 × 0 = −1, so areas stay the same size but the page is flipped. Flatten: 1 × 1 − 2 × 0.5 = 0, so the whole page is squashed onto a line and can't be unsquashed.
One motion after another
You can chain motions: first slant the page, then turn it. In math, "do B first, then A" is written A @ B, read right to left, like putting on socks (B) and then shoes (A).
Order matters. Turning then slanting is not the same as slanting then turning:
import numpy as np R = np.array([[0, -1], [1, 0]]) # rotate 90° S = np.array([[1, 1], [0, 1]]) # shear v = np.array([1, 0]) print("rotate, then shear:", S @ R @ v) print("shear, then rotate:", R @ S @ v) print("det of the combination:", round(np.linalg.det(S @ R), 3))
The same starting point ends up in two different places. And the combined motion's determinant is the two determinants multiplied together: areas change by one factor, then the other.
Show the answer
It flattens space into a lower dimension, so it can't be undone
Areas shrink to 0: the page collapses onto a line or a point, and different starting points collide.
Columns, not rows
A matrix's columns say where the basis arrows land. Reading its rows instead gives you the wrong picture.
Practice what you just read
- 3 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 4 review cards for spaced repetition
Try a quiz question
For A = [[3, 1], [2, 4]], where does the basis vector ĵ = [0, 1] land?