Just a grid of numbers
A matrix is a rectangular grid of numbers, arranged in rows and columns. That's the whole definition — nothing about it is more mysterious than a spreadsheet with the row and column headers stripped off. A matrix's size is always written rows × columns, in that order — a common early mix-up is flipping them, so it's worth saying out loud: rows first, then columns, every time. Click any cell below to see its own row and column number.
👆 Click a cell above.
The grid doesn't care what the numbers mean
A matrix is just a container — the same grid shape can hold completely different kinds of information. Two real ones:
A picture
Every digital image is a matrix — each cell's number is that pixel's brightness. The same 8×8 grid of numbers below is shown two ways: as plain numbers, and as an actual picture. Nothing was drawn by hand — the picture is the numbers, just shown as colored squares instead of digits.
A set of instructions for reshaping something
A matrix can also be a recipe: feed it a point, get back a new point, moved somewhere specific. Each button below sets a different 2×2 matrix — watch the square get rotated, stretched, or flipped by it. That's already a matrix multiplication happening — a 2×2 matrix times a single point's 2 numbers. Stage 3 shows exactly how, then scales it up.
Row, times column, added up
Here's the entire rule: to fill row i, column j of the answer, take row i of the first matrix and column j of the second — two equal-length lists — multiply position by position, and add up. That's a dot product, the same operation Attention uses to compare two words. Do that for every row/column pair and you've filled in the whole answer — click any cell in C below to see exactly which row and column made it.
Start small: 2×2 times 2×2
Both matrices are editable — change a number and watch the answer update live.
The same idea, just bigger: 4×4 times 4×4
Nothing about the rule changed — every cell of the answer is still one row dotted with one column. There are just more numbers to multiply and add per cell (4 terms instead of 2) and more cells to fill (16 instead of 4).
Different shapes: 1×2 times 2×4
Square matrices can hide a real requirement: the first matrix's columns have to match the second matrix's rows — here, 2 and 2. That shared length disappears from the answer's shape, which ends up 1 row × 4 columns — one row from A, but every column from B.
When does this actually work?
The rule above, stated in general: for an m×k matrix times a k×n matrix, the two k's in the middle have to match. What survives into the answer's shape is the two outer numbers, m and n. Try breaking it on purpose:
This is the one operation everything else here runs on
Neural networks (see Tonus) are, underneath the training and the jargon, layers of matrices multiplied one after another — every "neuron" is just one row of a dot product.
Attention (see Attention) reshapes every word's fingerprint into a Query, a Key, and a Value using exactly the multiplication from Stage 3 — three different matrices applied to the same starting numbers.
Computer graphics and games move, rotate, and scale every 3D model you've ever seen on screen using the same transformation idea from Stage 2 — just with bigger matrices, and many more points per shape.
Image filters (blur, sharpen, edge-detection) work by multiplying a small matrix across every patch of pixels in a photo — the same "picture is a matrix" idea from Stage 2, with a second matrix doing the reshaping.
A design for anyone who's ever gotten this wrong
If you've ever multiplied real matrices in code, you've seen this error. Download it, print it, put it wherever you want.
How this actually works — the precise version
For matrix A (size m×k) and matrix B (size k×n), the product C = AB (size m×n) is defined entry by entry:
C[i][j] = Σt=1..k A[i][t] × B[t][j]
Computing the whole matrix this way takes m × n cells, each one costing k multiplications and k−1 additions — for two square n×n matrices, that's O(n³) total, the naive algorithm every intro course teaches first. It's not the fastest one known: Strassen's algorithm (1969) does better by recombining the multiplications themselves, and faster algorithms have been found since — see "Learn more" below. Every demo on this page uses the plain O(n³) definition above; the answer is identical either way, just computed with more or fewer multiplications along the way.
Matrix multiplication is also not commutative: in general, AB ≠ BA — swap the order and you can get a completely different answer, or an operation that isn't even defined anymore (an m×k matrix times a k×n one works; flip the order and it only works if n happens to equal m too). Try it on the 2×2 demo in Stage 3: swap which grid holds which numbers and watch the answer change.
Learn more
Matrix multiplication on Wikipedia covers the general definition used on this page. Volker Strassen's 1969 paper introduced the first algorithm faster than the naive O(n³) approach shown above, by finding a way to compute a 2×2 block product with 7 multiplications instead of 8 — a result that still anchors most of the faster algorithms discovered since.