CourseVectors and matrices · Lesson 2 of 6

About this lesson

Four numbers that can stretch, turn, shear or flatten the whole plane — and power every layer of a network.

What you’ll cover

Open a step to read its text without the interactive.

  1. A matrix moves vectors

    A matrix is a grid of numbers. A 2×2 matrix M has rows [a, b] and [c, d], and multiplying it by a vector x = [x₁, x₂] gives a new vector:

    M·x = [a·x₁ + b·x₂, c·x₁ + d·x₂]

    Each output entry is the dot product of one row with x. With M’s rows [2, 1] and [0, 1], the vector [1, 2] goes to [2×1 + 1×2, 0×1 + 1×2] = [4, 2].

    The same rule applies to every point at once, so a matrix moves the whole plane. The faint grid is where points started; the darker grid shows where they end up.

    Before touching anything, predict where x lands if you set b to 0. Then slide b to 0 and check: M·x should become [2, 2].

  2. Columns are where î and ĵ land

    Call the two unit arrows along the axes î = [1, 0] and ĵ = [0, 1]. Multiply them by M and you get M’s first and second columns. So the columns of a matrix are just where î and ĵ land.

    That is enough to find where everything else goes. Any vector is x₁ steps of î plus x₂ steps of ĵ, so M·x = x₁·(column 1) + x₂·(column 2).

    Transformations that work like this are called linear: grid lines stay straight, parallel and evenly spaced, and the origin stays put.

    Drag the teal tip of î up to (1, 1) and leave ĵ alone. The matrix becomes rows [1, 0] and [1, 1], and the amber x follows: it is still 1 of the new î plus 2 of ĵ.

  3. A gallery of transformations

    A few matrices come up again and again. A stretch like rows [2, 0] and [0, 1] doubles every x coordinate. A rotation turns the plane about the origin: by an angle θ anticlockwise, its columns are [cos θ, sin θ] and [−sin θ, cos θ]. A shear slides rows of the grid sideways, like pushing the top of a deck of cards.

    A reflection mirrors the plane, and a projection flattens it onto a line. A projection throws information away: many points land on the same spot, so there is no way to undo it.

    The grey F has no symmetry, so you can always tell which way round it is.

    Press each preset in turn and watch the morph. Which one turns the F but leaves it readable, and which one mirrors it?

  4. The determinant: how areas scale

    Watch the amber square. It starts with area 1, and after the transformation it becomes a parallelogram. Its area is the size of the determinant, which for a 2×2 matrix is ad − bc.

    Every shape’s area scales by that same factor, |ad − bc|, because a linear transformation treats every patch of the grid alike. A stretch with determinant 2 doubles all areas.

    The sign carries information too. A negative determinant means the plane has been flipped over, so the F reads backwards. A determinant of 0 means the plane has been squashed onto a line or a single point, and nothing can undo that.

    Press Shear: the square slants but its area stays 1. Then drag ĵ onto the line through î, for example to (2, 0) when î is at (1, 0). The determinant hits 0 and the square collapses.

  5. One after another

    Apply A to x, then apply B to the result: B·(A·x). It turns out you can first combine the two into a single matrix, B·A, which does both jobs in one go. That is what matrix multiplication means: doing one transformation after another.

    The matrix written on the right acts first, just as in B·(A·x). Each entry of the product is a row of B dotted with a column of A.

    Order usually matters. Rotating and then shearing is not the same as shearing and then rotating, so in general A·B ≠ B·A. Determinants do multiply, though: det(B·A) = det(B) × det(A).

    With A = Rotate 90° and B = Shear, switch between the two orders and compare the products. Then look for a pair that gives the same matrix either way, for example Stretch and Reflect.

  6. A neural-network layer

    A layer of a neural network is mostly a matrix. Each row of the weight matrix W holds one neuron’s weights, so W·x computes every neuron’s weighted sum at once. Its entries are labelled w₁₁, w₁₂ (first row) and w₂₁, w₂₂ (second row) rather than a, b, c, d, because b now means something else: each neuron adds its own bias, and the biases b = [b₁, b₂] give W·x + b. This is the artificial neuron, many times over.

    The bias moves the whole picture, origin included. Finally a non-linear activation, here ReLU = max(0, z), acts on each entry separately.

    That last step is essential. Without it, two layers W₂(W₁·x + b₁) + b₂ collapse into a single matrix-plus-bias, so stacking layers would add nothing. ReLU bends the plane, and bends are what let deep networks shape complicated boundaries.

    Switch ReLU on. The output of x was (−1, 3); ReLU replaces the negative entry with 0, so it snaps to (0, 3). Then move the biases and watch which parts of the grid fold onto the axes.

Key terms

Matrix
A rectangular grid of numbers; multiplying a vector by it applies a linear transformation.
î and ĵ
The unit vectors [1, 0] and [0, 1] along the axes; a matrix’s columns are where they land.
Linear transformation
A way of moving every point that keeps grid lines straight, parallel and evenly spaced and leaves the origin fixed.
Determinant
For a 2×2 matrix, ad − bc: a signed area scale factor. Its size is the factor by which areas scale, and it is negative when the plane is flipped over.
Matrix multiplication
Combining two matrices into one that performs both transformations; in B·A, A acts first.
Bias
A number added to a neuron’s weighted sum before the activation, shifting it by the same amount whatever the inputs.
ReLU
The activation max(0, z): it keeps positive values and replaces negative ones with 0.

Sources