TL;DR
A matrix is a rectangular grid of numbers, arranged in rows and columns. It lets you handle a whole table of values as one object, and it describes operations that transform many numbers at once. A single matrix can say "rotate every point by 30 degrees" or hold every weight between two layers of a model.
Where you'll see it: a spreadsheet of measurements, a grayscale image where each pixel is one entry, and the weights connecting two layers of a neural network.
Intuition
A table with a name
Take a spreadsheet with four people in the rows and three measurements in the columns: height, weight and age. Written as a matrix, the whole table becomes one object with a name, say . Its size is written rows times columns, so this table is a matrix. The entry in row and column is written . Row first, then column: is the entry in row 2, column 3.
A matrix is also a stack of vectors. Each row is a vector, and so is each column. Looking at a matrix as a stack of row vectors tells you what each data point looks like. Looking at it as a stack of column vectors tells you what each measurement looks like across all the people.
A matrix as a machine
The more important role is that a matrix acts on vectors. Feed it a vector and it returns another vector. The matrix
takes any point to . It doubles every horizontal distance and leaves vertical distances alone. Because it acts on every point by the same simple rule (each output coordinate is a weighted sum of the input coordinates), it is a linear transformation.
flowchart LR A[Table of numbers] --> B[Matrix with m rows and n columns] B --> C[Store data] B --> D[Transform vectors by multiplying] B --> E[Hold the weights of a layer] B --> F[Write a system of equations]
Why it exists
Before matrix notation, a system of linear equations was written as a pile of separate equations, each with its own letters. For two unknowns that is easy. For a thousand unknowns the bookkeeping becomes hopeless. The matrix form writes the whole system as one statement, , and lets you reason about all of it at once. The same algebra works whether there are two unknowns or two million.
The math
Definition
An matrix is an array of real numbers with rows and columns, where and . We write . The entry sits in row and column .
Here is the set of real numbers, meaning ordinary numbers on the number line. A matrix with one column is a column vector, and a matrix with one row is a row vector. When the matrix is called square. By convention, matrices get capital letters and vectors get bold lowercase letters, such as .
Special matrices
- The zero matrix has every entry equal to 0.
- The identity matrix is square, with 1 on the diagonal and 0 everywhere else. For :
For every vector , . It is the matrix version of the number 1.
- The transpose swaps rows and columns: . An matrix becomes .
Basic operations
Matrices of the same shape can be added entry by entry: . Multiplying by a number scales every entry: . Shapes must match for addition. You cannot add a matrix to a matrix.
The most important operation is applying a matrix to a vector. For and , the product is a vector in with entries
So the -th entry of is the dot product of row of with . The number of columns of must equal the length of , and the number of rows of decides the length of the output.
Shapes follow one rule for matrix multiplication. An matrix times an matrix gives an matrix. The inner numbers must match, and the outer numbers give the result's shape. Most shape errors in code come from breaking this rule.
Some square matrices have an inverse with . Not every matrix has one. The Determinant tells you whether it does.
Worked examples
Let
- is and has 3 entries, so has 2 entries.
- Row 1 dotted with : .
- Row 2 dotted with : .
- So .
The matrix took a 3-entry input and produced a 2-entry output. The output size came from the rows of , not from the input.
A tiny image stores one brightness value per pixel, from 0 (black) to 255 (white):
- Row sums: , then , then .
- Total brightness: .
- There are 9 pixels, so the average is .
- The average is darker than mid-grey (128). The entry is the pixel in row 2, column 1.
Image processing works this way: the picture is a matrix, and every operation on it is arithmetic on its entries.
Take the matrix from the intuition section and the vector , which has length 5.
- .
- The first coordinate doubled and the second stayed the same.
- The new length is .
The matrix changed the geometry of the vector, not just its numbers. That is the point of treating matrices as transformations.
Tradeoffs and failure modes
A dense matrix is the right tool when most entries matter. It is the wrong tool in several common cases:
| Situation | What goes wrong | Better choice |
|---|---|---|
| Most entries are zero, such as a social network graph | A dense grid spends memory and time on zeros | A sparse format that stores only the nonzero entries |
| Solving for a large system | Computing explicitly is slow and loses accuracy | Factor once, for example with LU decomposition, then solve |
| Rows have different lengths | It is not a matrix, and padding it to a grid invents fake values | A list of vectors, or a padded array with a mask that marks the real entries |
| Data has three or more indices, such as video (time, row, column, colour) | A matrix has only two indices | A higher-dimensional array, usually called a tensor |
| Shapes do not line up | The program crashes, or the code runs and the result is meaningless | Check shapes at each step and name the dimensions |
Memory is a real constraint. A matrix of 8-byte numbers has entries, about 800 MB. Doubling the size quadruples the memory.
History
The word "matrix" was introduced by James Joseph Sylvester in 1850. He used it for a rectangular array that could give rise to smaller determinants, so the word meant something like "the thing that generates others". Arrays of numbers were in use long before that. The ancient Chinese text Nine Chapters on the Mathematical Art solved systems of equations by arranging counting rods in a grid, an early matrix-like method.
Arthur Cayley's A Memoir on the Theory of Matrices (1858) treated matrices as objects in their own right. It defined addition and multiplication of matrices, and multiplication was defined so that it corresponds to applying one transformation after another. Those are the rules used today. Gauss's elimination method for systems of equations, which is also a matrix procedure, had been in wide use for decades.
Matrices caught on because they unified a lot of linear algebra into one notation. They became central once computers could multiply them quickly, and then graphics hardware and neural networks made large matrix operations the core of modern computing.
Check yourself
QuestionWhat are the dimensions of a matrix with 5 rows and 2 columns, and what are the dimensions of its transpose?Show answer
The matrix is . Transposing swaps rows and columns, so the transpose is .
QuestionLet and . Compute .Show answer
Row 1: . Row 2: . So . Each output entry is one row dotted with .
QuestionWhy can you not add a matrix to a matrix?Show answer
Addition works entry by entry, which needs every entry of one matrix to have a partner in the same position in the other. A matrix has entries in column 3, and a matrix has no column 3. The two sets of positions do not match, so the sum is undefined. Both matrices must have the same shape.
QuestionA dataset has 1,000 customers and 20 features, stored as a matrix . Which expression gives the mean of each feature as a 20-entry vector?Show answer
Use , where is a vector of 1000 ones. The shapes check out: is , is , so the product is . Each entry is the sum of one feature's column, divided by 1000. The inner dimensions match, and the outer dimensions give the 20-entry result.
Sources
- Matrix (mathematics), Wikipedia: https://en.wikipedia.org/wiki/Matrix_(mathematics)
- Linear algebra, Wikipedia: https://en.wikipedia.org/wiki/Linear_algebra