Matrices and Markov Chains

☰ Contents

A matrix is a rectangle of numbers in rows and columns. Multiplying a matrix by a column of numbers gives a new column, so a matrix is a rule that turns one list into another, and a product of two matrices applies one rule after the other.

How are matrices described and added?

A matrix is described by its order: the number of rows, then the number of columns, so 2 rows and 3 columns make a 2 × 3 matrix. An entry is named by its row, then its column: a₂₁ sits in row 2, column 1. Matrices are added entry by entry, so they must have the same order. Writing rows with a semicolon between them, (1 4; 2 0) + (3 −1; 5 2) = (4 3; 7 2), since 1 + 3 = 4, 4 − 1 = 3, 2 + 5 = 7 and 0 + 2 = 2. This works because each position holds the same kind of quantity in both, such as one shop's sales of one item. Matrices of different orders cannot be added, since some entries would have no partner. A scalar multiplies every entry: 3 times (1 4; 2 0) is (3 12; 6 0). Check a sum by subtracting one matrix to recover the other.

3 × 4 = 12

multiplication is commutative: 3 × 4 = 4 × 3

Make 24 as a rectangle, then find another factor pair

A314275
Columns run down. Counting them second gives the order 2 × 3. Full lesson: The Order of a Matrix

See The order of a matrix and Adding, subtracting and scaling matrices.

A3105B14624567+=
Add entry by entry. The top left of the sum is 3 + 1 = 4. Full lesson: Adding, Subtracting and Scaling Matrices
3105142627+
A 2 × 2 and a 2 × 3 have different orders: the third column has nothing to add to, so this sum cannot be done. Full lesson: Adding, Subtracting and Scaling Matrices

Now you

A4559B1343+

What goes in row 1, column 2 of A + B?

48387×

Multiply this matrix by 4. What goes in row 1, column 1?

How does matrix multiplication work, and why is AB not BA?

To multiply two matrices, pair each row of the left matrix with each column of the right: multiply matching entries and add the products. For A = (1 2; 3 4) and B = (0 1; 1 0), row 1 of A with column 1 of B gives 1 × 0 + 2 × 1 = 2, and the full product is AB = (2 1; 4 3). The rule makes a product mean one transformation followed by another. AB usually differs from BA: BA = (3 4; 1 2), since B swaps the rows of A, while AB swaps its columns. Sizes can block a product: AB exists only when A has as many columns as B has rows, so a 2 × 3 times a 3 × 2 is a 2 × 2. Check one entry of BA: row 1 of B with column 1 of A gives 0 × 1 + 1 × 3 = 3.

A231402B541032×
Pair row 1 with column 1: multiply each pair and add, 2 × 5 + 3 × 1 + 1 × 3 = 16. Full lesson: The Matrix Product

A row of A must be as long as a column of B, and the outer numbers of the two orders give the order of the answer.

The rule is built this way because it totals a bill: a row of quantities times a column of prices is one shop's takings.

S4235price35×
Shop A sold 4 pens at $3 and 2 pads at $5, so its sales were 4 × 3 + 2 × 5 = $22. Full lesson: Combining Data with a Matrix Product

The other way round, a 3 × 2 times a 2 × 3 is a 3 × 3, so the two products need not even have the same order. Matrix multiplication is not commutative.

AB3211BA1213≠
So AB and BA are different matrices: matrix multiplication is not commutative. Full lesson: Why AB Differs from BA

The identity matrix I has ones down the main diagonal and zeros everywhere else, and multiplying by I changes nothing, just as multiplying a number by 1 does. The inverse of A is the matrix that multiplies with A to give I. A pattern in the powers of A is proved by induction: check the formula at n = 1, then multiply the assumed matrix by A once more. See The matrix product, Combining data with a matrix product, Why AB differs from BA, The identity and zero matrices and Matrix powers by induction.

Now you

3459267553×

Can this product be worked out?

814135167542713861×

What is the order of this product?

What does a determinant measure?

For a 2 × 2 matrix (a b; c d), the determinant is ad − bc: the product down the main diagonal minus the product along the other diagonal. It is the factor by which the matrix scales area. For (4 2; 5 3) the determinant is 4 × 3 − 2 × 5 = 12 − 10 = 2, so the unit square becomes a parallelogram of area 2. The formula gives area because the columns (4, 5) and (2, 3) are the sides of that parallelogram, and ad − bc is its area with a sign. A negative determinant means a reflection too. A determinant of 0 changes everything: the plane is squashed onto a line or a point, so the matrix has no inverse, as with (2 4; 1 2), where 2 × 2 − 4 × 1 = 0. Check: a shape of area 3 maps to area 3 × 2 = 6.

−2−1123412xy

det [[a, b], [0, 1]] = a × 1 − b × 0 = a, so b never appears: a shear leaves the area unchanged

Make the determinant 2

When it is 0, different points land on the same point, and nothing can send them back apart.

B2412
When the two products match, det = 0 and the matrix has no inverse. Full lesson: The Determinant of a 2 × 2 Matrix

When the determinant is not 0, the inverse of (a b; c d) takes three moves: swap a and d, change the signs of b and c, then divide every entry by ad − bc. Check by multiplying: the product is I.

A3512A⁻¹2−5−13→
Here ad − bc = 3 × 2 − 5 × 1 = 1, so the inverse of (3 5; 1 2) is (2 −5; −1 3). Full lesson: The Inverse of a 2 × 2 Matrix

The inverse solves simultaneous equations in one step. Write the pair as A X = b, with the coefficients in A, the unknowns in the column X and the constants in b, then multiply both sides on the left by A⁻¹ to leave X on its own.

A⁻¹2−5−13b11421×=
Work out A⁻¹b: 2 × 11 − 5 × 4 = 2 and −11 + 3 × 4 = 1, so x = 2 and y = 1. Full lesson: Solving Simultaneous Equations with a Matrix Inverse

A 3 × 3 determinant expands along a row: each entry of row 1 multiplies the 2 × 2 determinant left when its row and column are deleted, with the signs +, −, +. It measures volume, and it is 0 exactly when there is no inverse. The 3 × 3 inverse is the transposed matrix of cofactors divided by the determinant, and it solves three equations in one step. See The determinant of a 2 × 2 matrix, The inverse of a 2 × 2 matrix, Solving simultaneous equations with a matrix inverse, The determinant of a 3 × 3 matrix, The inverse of a 3 × 3 matrix and Solving three equations with a matrix inverse.

Now you

6353

What is the determinant of this matrix?

1326

Does this matrix have an inverse?

What are eigenvalues and eigenvectors?

An eigenvector of a square matrix A is a nonzero column that A only stretches, and the stretch factor is its eigenvalue: A v = λ v. Most columns change direction when multiplied by A = (4 −1; 2 1), but (1, 1) comes back as (3, 3), because 4 × 1 − 1 × 1 = 3 and 2 × 1 + 1 × 1 = 3, so (1, 1) is an eigenvector with eigenvalue 3. Any multiple of an eigenvector is another, so an eigenvector names a direction. A negative eigenvalue reverses it, and 0 collapses it. Eigenvectors matter because along them the matrix acts like plain multiplication, so powers are easy: applying A five times stretches (1, 1) by 3⁵. Finding them starts from the eigenvalues, the solutions of det(A − λI) = 0, and each eigenvalue then gives a direction. Test a claimed eigenvector: A times (1, 2) is (2, 4), twice the column, so the other eigenvalue is 2.

Avθ = 20°|Av| = 2.75turned by 16°−3−3−2−2−1−1112233

Av points 16° away from v, so Av ≠ λv: A turns this vector as well as stretching it, and it is not an eigenvector

Rotate v until Av lies on the same line as v

A4−1211133×=
But (1, 1) comes back as (3, 3): the same direction, stretched by a factor of 3. Full lesson: Eigenvalues and Eigenvectors

To find the eigenvalues, rewrite A v = λ v as (A − λI) v = 0. A non-zero column sent to zero means A − λI has no inverse, so det(A − λI) = 0. Subtracting λI takes λ off each diagonal entry, so here det(A − λI) = (4 − λ)(1 − λ) + 2 = λ² − 5λ + 6, the characteristic polynomial. Its roots, 2 and 3, are the eigenvalues.

or
Set it to zero and factor: the two eigenvalues of A are 2 and 3. Full lesson: The Characteristic Polynomial

For each eigenvalue, solve (A − λI) v = 0. At λ = 3 both rows give y = x, so v = (1, 1); at λ = 2 they give y = 2x, so v = (1, 2).

Put the eigenvectors as the columns of P and the eigenvalues, in the same order, on the diagonal of D. Then A P = P D, so A = P D P⁻¹. Powers follow, because every inner P⁻¹ P cancels: Aⁿ = P Dⁿ P⁻¹, and Dⁿ is found entry by entry. See Eigenvalues and eigenvectors, The characteristic polynomial, Finding an eigenvector, Diagonalizing a 2 × 2 matrix and Matrix powers by diagonalization.

P1112D⁵2430032P⁻¹2−1−11××
So A⁵ takes two matrix products, not four: P times D⁵, then times P⁻¹. Full lesson: Matrix Powers by Diagonalization

Now you

A − λI4 − λ−121 − λ

What does A − λI look like?

A1423

What is the characteristic polynomial of this matrix?

What is a Markov chain, and why does it settle down?

A Markov chain moves between a fixed set of states, and each move depends only on the current state. The move chances fill a transition matrix T, and T times one year's split gives the next. Suppose each year 0.2 of a city's people move to the country and 0.3 of the country's people move to the city. Each column of T is one starting state, so its entries add to 1. From 500 in each place, next year's city holds 400 + 150 = 550, since 4/5 of 500 stay and 3/10 of 500 arrive. A regular chain settles because T leaves one split unchanged, and every other part of the start shrinks at each step. Here that split has equal flows each way, which puts 3/5 of everyone in the city. A trap state, which nobody leaves, eventually holds everyone who can reach it. Test the settled split: 2/10 of 600 = 120 = 3/10 of 400.

AB100%0%0.3 →← 0.2πA = 0.4t104812t = 0: A = 100%

the split is still 60 percentage points from 40/60, and the gap halves at every step: (a₀ − 0.4) × 0.5ᵗ

Start with everything in A, then step time forward until the split freezes

After n years the split is Tⁿ times the start, and diagonalizing T makes Tⁿ easy to find.

T0.80.30.20.7now500500550450×=
Multiply T by this year’s split to get next year’s: 550 and 450. Full lesson: Transition Matrices

A transition diagram shows the same numbers as arrows between the states, with a loop for the share that stays. A chain is regular when some power of T has every entry positive. A state whose only arrow loops back to itself traps everyone who enters it, so that chain is not regular.

0.80.70.20.3CK
C is the city and K the country. Each arrow carries the share moving that way, and a loop carries the share that stays. Full lesson: Transition Diagrams

Start everyone in the city and the split settles. A settled split is one that T leaves unchanged, so it is an eigenvector of T with eigenvalue 1. Solve (T − I) s = 0: both rows give 2x = 3y, so the long-run split is 3 city to 2 country.

citycountryyear 010000year 1800200year 2700300year 3650350year 4625375
Start everyone in the city and the split settles: 800, 700, 650, 625. Full lesson: The Long-Run Steady State

Every transition matrix has eigenvalue 1, because its columns add to 1. The other eigenvalues are smaller than 1 in size, so their share of any start shrinks with each step, and the eigenvector for 1 is what remains. PageRank is this vector for the matrix of links between web pages. See Transition matrices, Transition diagrams and The long-run steady state.

Now you

0.80.1?0.9

80 percent of the city stays. What goes under it in column 1?

T0.80.30.20.7

In a transition matrix, which group of entries adds to 1?

Where this leads

Matrix arithmetic says what a matrix does to a column, the determinant says whether that can be undone, and eigenvalues say what happens when the matrix is applied many times. A Markov chain is that last question asked about a matrix of probabilities.

The mistakes worth naming

Matrices acting on shapes continue in congruence, circle theorems and transformations, and the characteristic polynomial uses the factoring from quadratics and polynomials. Related guides: Applications of Integration and Polar Curves and Complex Numbers.

Your turn

Three to try — tap what you get.

Multiplying any matrix by the identity gives

The determinant of [[2, 1], [3, 4]]

Each column of a Markov transition matrix sums to

Questions and answers

Why is AB not the same as BA for matrices?
Because a matrix represents an action, and actions do not generally commute. Rotating a book ninety degrees and then flipping it leaves it in a different position from flipping it first and then rotating. Matrix multiplication was defined to compose actions in sequence, so it inherits that order-dependence. For some pairs AB does equal BA, but it is a coincidence about those particular matrices, not a rule.
What does the determinant of a matrix tell you?
It is the factor by which the matrix scales area, in two dimensions, or volume in three. A determinant of 3 means every region comes out three times as large, and a negative determinant means the matrix also flipped orientation. A determinant of zero means the matrix squashed everything flat, which is why a matrix with zero determinant has no inverse: information was destroyed and cannot be recovered.
How do you use a matrix to solve simultaneous equations?
Write the system as a matrix of coefficients multiplied by a column of unknowns equalling a column of constants, then multiply both sides by the inverse matrix. The unknowns column is left alone on one side, so one inverse and one multiplication produce every unknown at once. If the determinant is zero the inverse does not exist, which corresponds exactly to the system having either no solution or infinitely many.
What is an eigenvector, in plain terms?
An eigenvector is a direction that the matrix does not turn — it only stretches or shrinks whatever lies along it. The factor by which it stretches is the eigenvalue. Most directions get rotated when a matrix acts on them; the eigenvectors are the special ones left pointing the same way, and they are what a repeated action eventually lines everything up with.
What is a Markov chain?
It is a model of a system that moves between a fixed set of states, where the chance of each next move depends only on the current state and not on the history. The probabilities are stored in a transition matrix, and multiplying by that matrix advances the model one step. Weather models, customer churn, board game positions and text prediction are all commonly modeled this way.
Why does a Markov chain settle down to a steady state?
Because repeated multiplication by the transition matrix pulls any starting distribution toward the matrix's dominant eigenvector. Every transition matrix has an eigenvalue of exactly 1, and the corresponding eigenvector is the distribution that the chain reproduces unchanged. Other components shrink with each step, so after enough steps the starting point stops mattering.
Mr. Chalk