Linear Algebra, Live
Strang §2.7 · Permutations

Permutation matrices: reorder the rows

Slide 5 turned a matrix on its side. This last slide meets the matrices that only reorder rows. Back in lesson 2, a zero pivot forced us to swap two rows. Now we write that swap as a matrix. Think of a playlist. The songs stay the same, only the order in which they play changes. A permutation matrix P does that to the rows of A: PA has the same rows as A, in a new order. P is easy to build: it is the identity I with its rows in that new order. To undo a reordering, you send every row back to its old place, and that turns out to be the transpose: P⁻¹ = Pᵀ.

Lesson 3 · slide 6Permutations
P = 001100010   PA = rows 3, 1, 2 of A
Key ideas
  1. A permutation matrix is I with its rows reordered. Each row and each column of P contains a single 1.
  2. PA reorders the rows of A; AP reorders its columns (see the rows-or-columns example below).
  3. P⁻¹ = Pᵀ: transposing undoes the reordering.
  4. With row exchanges, elimination gives PA = LU.

Build P from the identity

PA is like a playlist with slots: first, second, third. Each slot gets one row of A. To build P, write the rows of I in the order you want. For the order 3, 1, 2, row 1 of P is row 3 of I, and so on:

I = 100010001  →  P = rows 3, 1, 2 of I = 001100010
P is just I with its rows in the order 3, 1, 2. The circles show the single 1 in each row and each column.

Read P like this: a 1 in row i, column j means "slot i takes row j". The 1 at (1, 3) says slot 1 takes row 3.

P times A reorders the rows

Use a matrix A whose rows are full of 1s, 2s and 3s, so you can see where each row goes. Row 1 of PA is 001 times A: 0 of row 1, 0 of row 2 and 1 of row 3. That is row 3 of A.

P = 001100010   A = 111222333   PA = 333111222
Row 1 of P has its 1 in position 3, so every number in row 1 of PA comes from row 3 of A. Watch PA fill up with 3s, then 1s, then 2s.

On a single vector, P reorders the numbers: P·102030 = 301020.

Undoing a permutation: P⁻¹ = Pᵀ

To undo P, send each row back. If slot i took row j, the undo says "slot j takes row i back": a 1 at (j, i). Slot 1 took row 3, so the undo has a 1 at (3, 1). Moving every 1 from (i, j) to (j, i) flips P across its diagonal, which gives Pᵀ. So P⁻¹ = Pᵀ for every permutation matrix.

Pᵀ = 010001100: rows 2, 3, 1 of I
Pᵀ(PA) = Pᵀ 333111222 = rows 2, 3, 1 of that = 111222333 = A ✓
The same holds for P alone: PᵀP = I, so Pᵀ undoes P
Pᵀ times P. Each row of Pᵀ meets each column of P, and the result is the identity: the reordering is undone.

The 2 by 2 swap

In the plane the only reordering, apart from I, is the swap P = 0110. It trades the two numbers of a vector: P·xy = yx. Doing it twice puts them back, so P⁻¹ = P = Pᵀ. Its determinant (slide 2) is 0·0 − 1·1 = −1.

How many are there?

n rows can be ordered in n! = n·(n − 1)⋯1 ways. So there are 2! = 2 permutation matrices of size 2, and 3! = 6 of size 3, counting I. For 10 rows there are already 3,628,800.

Back to elimination: PA = LU

In lesson 2, a zero pivot meant exchanging rows. If A is invertible, run elimination once to discover which exchanges are needed. Collect them into one permutation P. Then PA can be eliminated with no exchanges at all: PA = LU.

A bigger example: a 3 × 3 that needs a swap

A = 011121279: the first pivot spot holds 0
Swap rows 1 and 2 with P = 010100001: PA = 121011279
row 3 − 2·row 1 → 037 (ℓ₃₁ = 2)   then row 3 − 3·row 2 → 004 (ℓ₃₂ = 3)
U = 121011004   L = 100010231 (ℓ₂₁ = 0)
Check row 3 of LU: 2·121 + 3·011 + 1·004 = 279, row 3 of PA ✓
First the swap, then ordinary elimination with no more trouble. The pivots 1, 1, 4 are all nonzero, so A has an inverse.

NumPy and other solvers compute this PA = LU form for matrices with thousands of rows. They even swap when they don't have to, picking the row with the largest pivot, because big pivots keep rounding errors small.

Next: The recap puts the whole lesson on one page.

Examples

Example 1 · Rows or columns

P = 0110, A = 1234.

Show solutionHide solution
PA = 3412: the rows swap
AP = 2143: the columns swap

Why columns? Column j of AP is A times column j of P (Ax by columns, lesson 1). Each column of P has a single 1, so it picks out one column of A. Column 1 of P is 01, which picks column 2 of A.

Example 2 · Reverse order

P = 001010100 puts rows in the order 3, 2, 1.

Show solutionHide solution
Pᵀ = P, so P⁻¹ = P: doing it twice restores the order

Example 3 · PA = LU

A = 0231 has a zero first pivot.

Show solutionHide solution
Swap: PA = 3102, already upper triangular
So PA = LU with L = I

Example 4

How many 3 by 3 permutation matrices are there? Which are their own inverse?

Show solutionHide solution

3! = 6. Four are their own inverse: I and the three single swaps. The other two, the orders 2, 3, 1 and 3, 1, 2, undo each other.

Example 5

What is the determinant of the 2 by 2 swap 0110?

Show solutionHide solution

det 0110 = 0·0 − 1·1 = −1, by slide 2's ad − bc. Lesson 6 explains the sign.

Exercise Build P that puts row 3 on top, then rows 1 and 2: the order 3, 1, 2. Edit the code and press Run.
slide_6.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Enjoying lesson 3? Get the full course: all 8 lessons through the SVD and PCA, your progress saved lesson by lesson, every solution and 5 extra problems per lesson. $20 once, yours for life.

Questions and comments

Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.

The full course

From Ax = b to PCA, by running it

Lessons 1 to 3 are free. Finish all eight and you will be able to:

You get: all 8 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · answers to your questions · every future update.

$20 once · lifetime access
Continue to checkout →

Already a member? Log in to the course