Search

Search papers and lessons

Matrix

Level 1 · 9 min read

A matrix is a rectangular grid of numbers that holds a whole table in one object, transforms vectors, or stores the weights of a model.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

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 AA. Its size is written rows times columns, so this table is a 4×34 \times 3 matrix. The entry in row ii and column jj is written AijA_{ij}. Row first, then column: A23A_{23} 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

S=(2001)S = \begin{pmatrix} 2 & 0 \\ 0 & 1 \end{pmatrix}

takes any point (x,y)(x, y) to (2x,y)(2x, y). 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, Ax=bA\mathbf{x} = \mathbf{b}, 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

Definition: Matrix

An m×nm \times n matrix is an array of real numbers A=(Aij)A = (A_{ij}) with mm rows and nn columns, where i=1,…,mi = 1, \dots, m and j=1,…,nj = 1, \dots, n. We write A∈Rm×nA \in \mathbb{R}^{m \times n}. The entry AijA_{ij} sits in row ii and column jj.

Here R\mathbb{R} 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 m=nm = n the matrix is called square. By convention, matrices get capital letters and vectors get bold lowercase letters, such as x\mathbf{x}.

Special matrices

  • The zero matrix has every entry equal to 0.
  • The identity matrix InI_n is square, with 1 on the diagonal and 0 everywhere else. For n=2n = 2:
I2=(1001)I_2 = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}

For every vector x\mathbf{x}, Inx=xI_n \mathbf{x} = \mathbf{x}. It is the matrix version of the number 1.

  • The transpose A⊤A^\top swaps rows and columns: (A⊤)ij=Aji(A^\top)_{ij} = A_{ji}. An m×nm \times n matrix becomes n×mn \times m.

Basic operations

Matrices of the same shape can be added entry by entry: (A+B)ij=Aij+Bij(A + B)_{ij} = A_{ij} + B_{ij}. Multiplying by a number cc scales every entry: (cA)ij=c Aij(cA)_{ij} = c\,A_{ij}. Shapes must match for addition. You cannot add a 2×32 \times 3 matrix to a 3×23 \times 2 matrix.

The most important operation is applying a matrix to a vector. For A∈Rm×nA \in \mathbb{R}^{m \times n} and x∈Rn\mathbf{x} \in \mathbb{R}^{n}, the product AxA\mathbf{x} is a vector in Rm\mathbb{R}^{m} with entries

(Ax)i=∑j=1nAij xj.(A\mathbf{x})_i = \sum_{j=1}^{n} A_{ij}\, x_j .

So the ii-th entry of AxA\mathbf{x} is the dot product of row ii of AA with x\mathbf{x}. The number of columns of AA must equal the length of x\mathbf{x}, and the number of rows of AA decides the length of the output.

Shapes follow one rule for matrix multiplication. An m×nm \times n matrix times an n×pn \times p matrix gives an m×pm \times p 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 A−1A^{-1} with A−1A=IA^{-1}A = I. Not every matrix has one. The Determinant tells you whether it does.

Worked examples

Example
A matrix times a vector, by hand

Let

A=(123456),x=(1,0,−1).A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}, \qquad \mathbf{x} = (1, 0, -1).
  1. AA is 2×32 \times 3 and x\mathbf{x} has 3 entries, so AxA\mathbf{x} has 2 entries.
  2. Row 1 dotted with x\mathbf{x}: 1⋅1+2⋅0+3⋅(−1)=1+0−3=−21 \cdot 1 + 2 \cdot 0 + 3 \cdot (-1) = 1 + 0 - 3 = -2.
  3. Row 2 dotted with x\mathbf{x}: 4⋅1+5⋅0+6⋅(−1)=4+0−6=−24 \cdot 1 + 5 \cdot 0 + 6 \cdot (-1) = 4 + 0 - 6 = -2.
  4. So Ax=(−2,−2)A\mathbf{x} = (-2, -2).

The matrix took a 3-entry input and produced a 2-entry output. The output size came from the rows of AA, not from the input.

Example
A grayscale image as a matrix

A tiny 3×33 \times 3 image stores one brightness value per pixel, from 0 (black) to 255 (white):

A=(01282556464642551280).A = \begin{pmatrix} 0 & 128 & 255 \\ 64 & 64 & 64 \\ 255 & 128 & 0 \end{pmatrix}.
  1. Row sums: 0+128+255=3830 + 128 + 255 = 383, then 64+64+64=19264 + 64 + 64 = 192, then 255+128+0=383255 + 128 + 0 = 383.
  2. Total brightness: 383+192+383=958383 + 192 + 383 = 958.
  3. There are 9 pixels, so the average is 958/9≈106.4958 / 9 \approx 106.4.
  4. The average is darker than mid-grey (128). The entry A21=64A_{21} = 64 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.

Example
A matrix that stretches a vector

Take the matrix SS from the intuition section and the vector v=(3,4)\mathbf{v} = (3, 4), which has length 5.

  1. Sv=(2⋅3+0⋅4, 0⋅3+1⋅4)=(6,4)S\mathbf{v} = (2 \cdot 3 + 0 \cdot 4,\ 0 \cdot 3 + 1 \cdot 4) = (6, 4).
  2. The first coordinate doubled and the second stayed the same.
  3. The new length is 62+42=52≈7.21\sqrt{6^2 + 4^2} = \sqrt{52} \approx 7.21.

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:

SituationWhat goes wrongBetter choice
Most entries are zero, such as a social network graphA dense grid spends memory and time on zerosA sparse format that stores only the nonzero entries
Solving Ax=bA\mathbf{x} = \mathbf{b} for a large systemComputing A−1A^{-1} explicitly is slow and loses accuracyFactor AA once, for example with LU decomposition, then solve
Rows have different lengthsIt is not a matrix, and padding it to a grid invents fake valuesA 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 indicesA higher-dimensional array, usually called a tensor
Shapes do not line upThe program crashes, or the code runs and the result is meaninglessCheck shapes at each step and name the dimensions

Memory is a real constraint. A 10,000×10,00010{,}000 \times 10{,}000 matrix of 8-byte numbers has 10810^8 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 5×25 \times 2. Transposing swaps rows and columns, so the transpose is 2×52 \times 5.

QuestionLet A=(2103)A = \begin{pmatrix} 2 & 1 \\ 0 & 3 \end{pmatrix} and x=(4,−1)\mathbf{x} = (4, -1). Compute AxA\mathbf{x}.Show answer

Row 1: 2⋅4+1⋅(−1)=72 \cdot 4 + 1 \cdot (-1) = 7. Row 2: 0⋅4+3⋅(−1)=−30 \cdot 4 + 3 \cdot (-1) = -3. So Ax=(7,−3)A\mathbf{x} = (7, -3). Each output entry is one row dotted with x\mathbf{x}.

QuestionWhy can you not add a 2×32 \times 3 matrix to a 3×23 \times 2 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 2×32 \times 3 matrix has entries in column 3, and a 3×23 \times 2 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 1000×201000 \times 20 matrix XX. Which expression gives the mean of each feature as a 20-entry vector?Show answer

Use 11000X⊤1\frac{1}{1000} X^\top \mathbf{1}, where 1\mathbf{1} is a vector of 1000 ones. The shapes check out: X⊤X^\top is 20×100020 \times 1000, 1\mathbf{1} is 1000×11000 \times 1, so the product is 20×120 \times 1. 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