Linear Algebra, Live
Strang §2.4 · Rules for matrix operations

Multiplying two matrices

Slide 5 multiplied an elimination matrix E by A with one rule: each row of E combines the rows of A. Now we see how any matrix times any matrix works. Lesson 1 already multiplied a matrix by a vector, and that is almost all we need. The idea is simple: B is just a few vectors standing side by side, its columns. Multiply A by each column of B, and stand the answers side by side. That is AB. To get any single number in AB, take a row of A and a column of B and find their dot product. Same rules as lesson 1, done several times.

Lesson 2 · slide 6Matrix times matrix
(AB)ij = (row i of A) · (column j of B)     AB = [ A·(column 1 of B)   A·(column 2 of B) ]
Key ideas
  1. One entry: the number in row i, column j of AB is row i of A dotted with column j of B.
  2. One column: column j of AB is A times column j of B, a combination of the columns of A.
  3. Sizes: (m × n) times (n × p) gives m × p. The inside numbers must match.
  4. Order matters: usually AB ≠ BA.

One number at a time: row times column

Take

A = 1234    B = 5678

The top-left number of AB comes from row 1 of A and column 1 of B. Multiply matching parts and add, a dot product (lesson 1, slide 2):

row 1 · column 1 = 12 · 57 = 1·5 + 2·7 = 5 + 14 = 19

Do the same for every pair. Row i and column j make the number in row i, column j:

row 1 · column 2 = 1·6 + 2·8 = 22
row 2 · column 1 = 3·5 + 4·7 = 43
row 2 · column 2 = 3·6 + 4·8 = 50
AB = 19224350
The highlighted row of A meets the highlighted column of B. Their dot product fills one number of AB.

One column at a time: A times each column of B

Now look at a whole column of AB. Column 1 of AB is just A times column 1 of B, a matrix times a vector, exactly as in lesson 1. By columns:

A·57 = 5·13 + 7·24 = 5 + 1415 + 28 = 1943

That is column 1 of AB. Column 2 is A·68 = 2250. So every column of AB is a combination of the columns of A.

Output AB
1943
2250
←
A: the same two rows each time
out top = 1·(in top) + 2·(in bottom)out bottom = 3·(in top) + 4·(in bottom)
←
B: two inputs side by side
57
68
B is just two inputs standing side by side. A works on each one, and the answers stand side by side in AB.

One row at a time

There is a mirror image of this. Row 1 of AB is row 1 of A times B: take 1 of B's first row plus 2 of B's second row:

1·56 + 2·78 = 1922

All three views add up the same products. Use row times column to compute by hand. Use the column view to understand what AB is.

The same with 3 × 3 matrices

Nothing new happens with bigger matrices. Each dot product just has three products to add instead of two. Watch all nine numbers of AB fill in:

A = 120013201    B = 102210041
row 1 · column 1 = 1·1 + 2·2 + 0·0 = 5    row 2 · column 2 = 0·0 + 1·1 + 3·4 = 13
AB = 5222133245
3 × 3 times 3 × 3: nine numbers, each one a row of A dotted with a column of B.

Not every pair can be multiplied

A row of A must have as many numbers as a column of B, or the dot product doesn't work. So the number of columns of A must equal the number of rows of B. If they don't match, AB simply does not exist:

(m × n) · (n × p) → (m × p)    the inside numbers match, the outside numbers give the size of AB
(2 × 3) · (3 × 2) → 2 × 2 ✓    (3 × 2) · (2 × 4) → 3 × 4 ✓
(2 × 3) · (2 × 3): a row of A has 3 numbers, a column of B has only 2. ✗ No product.

So with A = 123456, the product AA does not exist, even though A times itself sounds natural. Only square matrices (same number of rows and columns) can always be multiplied by themselves. And sometimes AB exists but BA doesn't: a 2 × 3 times a 3 × 4 works, but 3 × 4 times 2 × 3 does not.

A vector is a matrix with one column, so lesson 1's Ax is the case p = 1.

Order matters

Multiply the same two matrices the other way round:

BA = 5678 1234 = 23343146    but AB = 19224350

Different. With numbers, 3·5 = 5·3. With matrices, usually AB ≠ BA, so always keep the order. Other rules do carry over: (AB)C = A(BC), so the brackets can move, and A(B + C) = AB + AC (add matrices entry by entry).

A bigger example: 2 × 3 times 3 × 2

A = 120013    B = 102104
row 1 · column 1 = 1·1 + 2·2 + 0·0 = 5    row 1 · column 2 = 1·0 + 2·1 + 0·4 = 2
row 2 · column 1 = 0·1 + 1·2 + 3·0 = 2    row 2 · column 2 = 0·0 + 1·1 + 3·4 = 13
AB = 52213    (2 × 3) · (3 × 2) → (2 × 2)

Each dot product now has 3 products to add. A neural network layer is one big matrix product like this, with thousands of rows and columns.

Where this is used

Slide 5 already used it: each elimination step is a matrix, and its steps E₃₂E₃₁E₂₁A are a chain of these products. The next slide turns that chain around into A = LU. Graphics, robotics and machine learning chain matrices the same way.

Next: Elimination turned A into the triangle U. Can we get A back from U? Undoing the steps, as a product of matrices, gives the factorization A = LU.

Examples

Example 1 · Row times column

Find AB for A = 2013, B = 1421.

Show solutionHide solution
row 1 · col 1 = 2·1 + 0·2 = 2    row 1 · col 2 = 2·4 + 0·1 = 8
row 2 · col 1 = 1·1 + 3·2 = 7    row 2 · col 2 = 1·4 + 3·1 = 7
AB = 2877

Example 2 · Column by column

A = 1234, B = 1011. Find AB one column at a time.

Show solutionHide solution
Column 1 = A·11 = 1·13 + 1·24 = 37
Column 2 = A·01 = column 2 of A = 24
AB = 3274

Example 3 · Two orders

A = 1101, B = 1011.

Show solutionHide solution
AB = 2111    BA = 1112

Different, so AB ≠ BA here.

Example 4

A is 3 × 2 and B is 2 × 4. What size is AB? Can you compute BA?

Show solutionHide solution

AB is 3 × 4 (the inside 2s match). BA would be (2 × 4)·(3 × 2): the inside numbers 4 and 3 don't match, so BA doesn't exist.

Example 5

Find only the first column of AB for A = 1234 and B = 1011.

Show solutionHide solution

Column 1 of AB = A times column 1 of B = A·11 = 1 + 23 + 4 = 37.

Exercise Change B in the code to [[0, 1], [1, 0]] and run it to show A, B and AB with table(). Then look: what did B do to the columns of A? 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 2? 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