Index Ai Academy
Phase 2 · Math, when you need it · Linear algebra

Matrices as transformations

A matrix stretches, rotates and projects space.

About 25 minutes · A free account opens the whole lesson

Create a free account Try part of it here

Already have an account? Sign in to open this lesson

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

  1. A matrix moves points
  2. The trick: watch two arrows
  3. A small zoo of motions
  4. The determinant: how much does area change?
  5. Worked example: three determinants by hand
  6. One motion after another
  7. 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 ı^\hat{\imath} ("i-hat", one step right) and ȷ^\hat{\jmath} ("j-hat", one step up):

A=[2101]⇒Aı^=[20],  Aȷ^=[11]A = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix} \quad\Rightarrow\quad A\hat{\imath} = \begin{bmatrix} 2 \\ 0 \end{bmatrix},\ \ A\hat{\jmath} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}

Read it as: "A sends i-hat to its first column and j-hat to its second column."

Try it now

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.

Quick check
A matrix's columns are [3, 0] and [0, 3]. What does it do to the page?
  1. Turns the page by 3 degrees
  2. Makes everything 3 times bigger in both directions
  3. Flattens the page onto a line
  4. Moves everything 3 steps right
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 doesColumns (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°
It runs right here in your browser, with nothing to install.

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
det⁡[abcd]=ad−bc\det \begin{bmatrix} a & b \\ c & d \end{bmatrix} = ad - bc

Read it as: "the determinant is the top-left times the bottom-right, minus the top-right times the bottom-left."

Quick check
The matrix [[2, 0], [0, 3]] stretches x by 2 and y by 3. Its determinant is a·d − b·c. What does this print?
a, b, c, d = 2, 0, 0, 3   # the matrix [[a, b], [c, d]]
print(a * d - b * c)
  1. 0
  2. 5
  3. -6
  4. 6
Show the answer

6

The unit square becomes a 2 by 3 rectangle, so every area is multiplied by 6.

Worked example

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))
In the lesson you can change this and run it.

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.

Quick check
A matrix has determinant 0. What does that tell you?
  1. It doesn't change anything
  2. It flattens space into a lower dimension, so it can't be undone
  3. It rotates by 0 degrees
  4. Its entries are all zero
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

Create a free account

Try a quiz question

For A = [[3, 1], [2, 4]], where does the basis vector ĵ = [0, 1] land?

  1. [1, 4]
  2. [3, 2]
  3. [2, 4]
  4. [3, 1]

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson