Skip to content

Matrix multiplication

Do one map, then another, and get a single matrix; compute it three ways, and see why the order of the two matters.

Helps to know firstA matrix moves the plane

Helps to know first
This topic

First, the picture

Turn then shear, or shear then turn: the same two moves in two orders. Watch the two pictures end up different.

Order matters

A quarter turn and a shear, in the two orders.

Two copies of the same picture. Each gets the same two moves, a quarter turn and a shear, in opposite orders.

0.00 / 10.40 s
Describe this picture

Two panels side by side (one above the other on a phone), each with a faint square grid and the same letter F on it. On the left the grid turns a quarter turn anticlockwise and then shears, sliding each row sideways by its height. On the right it shears first and then turns. Each panel holds its end picture, and its single matrix appears in its corner: on the left [1−110]\begin{bmatrix}1&-1\\1&0\end{bmatrix}, on the right [0−111]\begin{bmatrix}0&-1\\1&1\end{bmatrix}. The two end pictures differ. A transport plays, pauses and steps; there is no other control.

One map, then another

In A matrix moves the plane a matrix was the table of where the two unit arrows e1=(1,0)\mathbf{e}_1=(1,0) and e2=(0,1)\mathbf{e}_2=(0,1) land: the first column says where e1\mathbf{e}_1 goes, the second where e2\mathbf{e}_2 goes. This page uses two such maps.

The first is a quarter turn anticlockwise. It sends e1\mathbf{e}_1 to (0,1)(0,1) and e2\mathbf{e}_2 to (−1,0)(-1,0):

Arot=[0−110].\mathbf{A}_\text{rot}=\begin{bmatrix}0&-1\\1&0\end{bmatrix}.

The second is a shear. It slides every point sideways by its height, so (x,y)(x,y) goes to (x+y, y)(x+y,\,y). It leaves e1\mathbf{e}_1 where it is and pushes e2\mathbf{e}_2 over to (1,1)(1,1):

Ashear=[1101].\mathbf{A}_\text{shear}=\begin{bmatrix}1&1\\0&1\end{bmatrix}.

Now do one, then the other. Turn first, then shear. Where do the unit arrows end up? Follow each one through both maps:

  • e1\mathbf{e}_1 turns to (0,1)(0,1), and the shear pushes that to (0+1, 1)=(1,1)(0+1,\,1)=(1,1).
  • e2\mathbf{e}_2 turns to (−1,0)(-1,0), and the shear leaves it at (−1+0, 0)=(−1,0)(-1+0,\,0)=(-1,0).

Two arrows and their landing places: that is a matrix. The single map “turn, then shear” is

[1−110].\begin{bmatrix}1&-1\\1&0\end{bmatrix}.

It is the left panel’s end state at the top of the page. This matrix is the product of the two, written AshearArot\mathbf{A}_\text{shear}\mathbf{A}_\text{rot}.

Why the order is written backwards

Look at the order of the names. The turn happens first, but it is written on the right. Here is why. A point x\mathbf{x} is turned to Arotx\mathbf{A}_\text{rot}\mathbf{x}, and then that is sheared:

Ashear(Arot x)=(AshearArot) x.\mathbf{A}_\text{shear}\big(\mathbf{A}_\text{rot}\,\mathbf{x}\big)=\big(\mathbf{A}_\text{shear}\mathbf{A}_\text{rot}\big)\,\mathbf{x}.

The first map stands next to x\mathbf{x}, because it touches x\mathbf{x} first. So read a product from right to left: AB\mathbf{A}\mathbf{B} means “do B\mathbf{B}, then do A\mathbf{A}”.

One check remains. Is “one map, then another” always a matrix? It is, because each map keeps the linear rule from A matrix moves the plane. If B\mathbf{B} keeps scaling and adding, and so does A\mathbf{A}, then

A(B(c x+d y))=A(c Bx+d By)=c ABx+d ABy.\begin{aligned} &\mathbf{A}\big(\mathbf{B}(c\,\mathbf{x}+d\,\mathbf{y})\big)\\ &\quad=\mathbf{A}\big(c\,\mathbf{B}\mathbf{x}+d\,\mathbf{B}\mathbf{y}\big)\\ &\quad=c\,\mathbf{A}\mathbf{B}\mathbf{x}+d\,\mathbf{A}\mathbf{B}\mathbf{y}. \end{aligned}

So the chain is linear too, and a linear map of the plane is fixed by where it sends the two unit arrows.

The other order

Now shear first, then turn:

  • e1\mathbf{e}_1 is not moved by the shear, and the turn sends it to (0,1)(0,1).
  • e2\mathbf{e}_2 is sheared to (1,1)(1,1), and the turn sends that to (−1,1)(-1,1).
ArotAshear=[0−111].\mathbf{A}_\text{rot}\mathbf{A}_\text{shear}=\begin{bmatrix}0&-1\\1&1\end{bmatrix}.

That is not [1−110]\begin{bmatrix}1&-1\\1&0\end{bmatrix}, the matrix of the first order.

Watch the clip again and compare the two end pictures: the same picture, the same two moves, two different results. In general AB≠BA\mathbf{A}\mathbf{B}\ne\mathbf{B}\mathbf{A}. Some pairs do agree, such as two turns, but you can’t count on it.

Columns of the product

The same reasoning works for any two matrices. Column qq of B\mathbf{B}, which I call bq\mathbf{b}_q, is where B\mathbf{B} sends the unit arrow eq\mathbf{e}_q. Then A\mathbf{A} sends it on to Abq\mathbf{A}\mathbf{b}_q. So:

column q of AB  is  Abq.\text{column } q \text{ of } \mathbf{A}\mathbf{B}\ \text{ is }\ \mathbf{A}\mathbf{b}_q .

To multiply two matrices, multiply the first by each column of the second, one at a time. Each of those is a matrix times a vector, which you already know how to do.

Columns of the product

A = [1 2; 3 4] moves the columns of B = [0 1; 1 0].

The columns of B as two arrows, b_1 = (0, 1) and b_2 = (1, 0). The grid is about to move by A.

A b_1
(0, 1)
A b_2
(1, 0)
0.00 / 9.60 s
Describe this picture

A grid with the two columns of B=[0110]\mathbf{B}=\begin{bmatrix}0&1\\1&0\end{bmatrix}, b1=(0,1)\mathbf{b}_1=(0,1) and b2=(1,0)\mathbf{b}_2=(1,0), drawn as arrows from the origin. The grid then moves by A=[1234]\mathbf{A}=\begin{bmatrix}1&2\\3&4\end{bmatrix}. This A\mathbf{A} flips the plane over, so the grid first stretches, then folds across a dotted mirror line, lying flat on it for a moment, and opens out. Each arrow is carried to A\mathbf{A} times that column, (2,4)(2,4) and (1,3)(1,3). Those two arrows are the columns of AB\mathbf{A}\mathbf{B}, and they are written into the product matrix in the corner one column at a time, [2143]\begin{bmatrix}2&1\\4&3\end{bmatrix}. Readouts give where the two arrows are. There is no control.

Watch where the two arrows land. Each one is a column of B\mathbf{B} pushed through A\mathbf{A}, and nothing else.

Shapes that fit

Matrices need not be square. Say A\mathbf{A} is m×nm\times n: mm rows and nn columns, so it takes nn numbers in. Each column of B\mathbf{B} goes into A\mathbf{A}, so each column of B\mathbf{B} must have nn numbers: B\mathbf{B} has nn rows. Let B\mathbf{B} have KK columns; on this page KK is only that count. Then

A⏟m×n  B⏟n×K=AB⏟m×K.\underbrace{\mathbf{A}}_{m\times n}\;\underbrace{\mathbf{B}}_{n\times K}=\underbrace{\mathbf{A}\mathbf{B}}_{m\times K}.

The two inside numbers must match, and the two outside numbers give the shape of the answer. A 2×32\times3 matrix can’t multiply another 2×32\times3: it takes 3 numbers in, and the columns of the second have only 2.

Row times column

Each column Abq\mathbf{A}\mathbf{b}_q is a matrix times a vector, and A matrix moves the plane computed that one row at a time: entry pp is row pp of A\mathbf{A} dotted with the vector. So one entry of the product is one row of A\mathbf{A} dotted with one column of B\mathbf{B}:

(AB)pq=∑k=1napk bkq=ap1b1q+⋯+apnbnq.\begin{aligned} (\mathbf{A}\mathbf{B})_{pq}&=\sum_{k=1}^{n}a_{pk}\,b_{kq}\\ &=a_{p1}b_{1q}+\dots+a_{pn}b_{nq}. \end{aligned}

Here apka_{pk} is the entry of A\mathbf{A} in row pp and column kk, counted from 1, and kk runs along the row of A\mathbf{A} and down the column of B\mathbf{B} together.

Try it on A=[1234]\mathbf{A}=\begin{bmatrix}1&2\\3&4\end{bmatrix} and B=[0110]\mathbf{B}=\begin{bmatrix}0&1\\1&0\end{bmatrix}:

AB=[1⋅0+2⋅11⋅1+2⋅03⋅0+4⋅13⋅1+4⋅0]=[2143].\begin{aligned} \mathbf{A}\mathbf{B}&=\begin{bmatrix}1\cdot0+2\cdot1&1\cdot1+2\cdot0\\3\cdot0+4\cdot1&3\cdot1+4\cdot0\end{bmatrix}\\ &=\begin{bmatrix}2&1\\4&3\end{bmatrix}. \end{aligned}

The answer is A\mathbf{A} with its two columns swapped. That makes sense from the columns picture: B\mathbf{B} sends e1\mathbf{e}_1 to e2\mathbf{e}_2 and e2\mathbf{e}_2 to e1\mathbf{e}_1, so the product’s first column is A\mathbf{A}‘s second.

Watch out

A product is not entry times entry. Multiplying the entries in the same places gives [1⋅02⋅13⋅14⋅0]=[0230]\begin{bmatrix}1\cdot0&2\cdot1\\3\cdot1&4\cdot0\end{bmatrix}=\begin{bmatrix}0&2\\3&0\end{bmatrix}, which is not AB\mathbf{A}\mathbf{B} and is not “one map, then another”. Every entry of a product is a whole row against a whole column.

Column times row

There is a third way to get the same numbers. Take column kk of A\mathbf{A} and row kk of B\mathbf{B}. A column of mm numbers times a row of KK numbers makes a full m×Km\times K table: the entry in row pp and column qq is apkbkqa_{pk}b_{kq}. Add these tables for k=1k=1 to nn, and each entry collects ap1b1q+⋯+apnbnqa_{p1}b_{1q}+\dots+a_{pn}b_{nq}, the same sum as before.

For the same A\mathbf{A} and B\mathbf{B}:

[13][01]+[24][10]=[0103]+[2040]=[2143].\begin{aligned} &\begin{bmatrix}1\\3\end{bmatrix}\begin{bmatrix}0&1\end{bmatrix}+\begin{bmatrix}2\\4\end{bmatrix}\begin{bmatrix}1&0\end{bmatrix}\\ &\quad=\begin{bmatrix}0&1\\0&3\end{bmatrix}+\begin{bmatrix}2&0\\4&0\end{bmatrix}=\begin{bmatrix}2&1\\4&3\end{bmatrix}. \end{aligned}

Three ways, one answer. Columns tell you what the product does to the unit arrows. Rows times columns are the quickest by hand. Columns times rows show a product as a sum of simple pieces, a view that comes back often in linear algebra.

The rules

Grouping does not matter. (AB)C=A(BC)(\mathbf{A}\mathbf{B})\mathbf{C}=\mathbf{A}(\mathbf{B}\mathbf{C}). Both sides mean “do C\mathbf{C}, then B\mathbf{B}, then A\mathbf{A}”, so they are the same chain of maps, and you may drop the brackets: ABC\mathbf{A}\mathbf{B}\mathbf{C}.

Order does matter. AB\mathbf{A}\mathbf{B} and BA\mathbf{B}\mathbf{A} are usually different, as the two panels showed. For the A\mathbf{A} and B\mathbf{B} above, BA=[3412]\mathbf{B}\mathbf{A}=\begin{bmatrix}3&4\\1&2\end{bmatrix}: on the left, B\mathbf{B} swaps the rows of A\mathbf{A} instead of the columns.

The identity changes nothing. The map that leaves both unit arrows where they are is the identity matrix,

I=[1001],AI=IA=A.\mathbf{I}=\begin{bmatrix}1&0\\0&1\end{bmatrix},\qquad \mathbf{A}\mathbf{I}=\mathbf{I}\mathbf{A}=\mathbf{A}.

Doing nothing before or after a map leaves the map as it was. For nn numbers, I\mathbf{I} is n×nn\times n with 1s down the diagonal and 0s elsewhere.

Blocks multiply like entries. Cut A\mathbf{A} into a left and a right block of columns, A1\mathbf{A}_1 and A2\mathbf{A}_2, and cut B\mathbf{B} into a top and a bottom block of rows, B1\mathbf{B}_1 and B2\mathbf{B}_2, at the same place. Then

[A1A2][B1B2]=A1B1+A2B2,\begin{bmatrix}\mathbf{A}_1&\mathbf{A}_2\end{bmatrix}\begin{bmatrix}\mathbf{B}_1\\\mathbf{B}_2\end{bmatrix}=\mathbf{A}_1\mathbf{B}_1+\mathbf{A}_2\mathbf{B}_2 ,

the column-times-row rule with blocks in place of single columns and rows. The order inside each block product stays as it was.

Worked example

1. The two orders. Rotate, then shear:

AshearArot=[1101][0−110]=[1−110].\begin{aligned} \mathbf{A}_\text{shear}\mathbf{A}_\text{rot}&=\begin{bmatrix}1&1\\0&1\end{bmatrix}\begin{bmatrix}0&-1\\1&0\end{bmatrix}\\ &=\begin{bmatrix}1&-1\\1&0\end{bmatrix}. \end{aligned}

Entry by entry: row 1 times column 1 is 1⋅0+1⋅1=11\cdot0+1\cdot1=1, row 1 times column 2 is 1⋅(−1)+1⋅0=−11\cdot(-1)+1\cdot0=-1, and row 2 gives 0⋅0+1⋅1=10\cdot0+1\cdot1=1 and 0⋅(−1)+1⋅0=00\cdot(-1)+1\cdot0=0.

Check with the arrows: e1→(0,1)→(1,1)\mathbf{e}_1\to(0,1)\to(1,1) and e2→(−1,0)→(−1,0)\mathbf{e}_2\to(-1,0)\to(-1,0), the two columns. Shear, then rotate: ArotAshear=[0−111]\mathbf{A}_\text{rot}\mathbf{A}_\text{shear}=\begin{bmatrix}0&-1\\1&1\end{bmatrix}, with e1→(1,0)→(0,1)\mathbf{e}_1\to(1,0)\to(0,1) and e2→(1,1)→(−1,1)\mathbf{e}_2\to(1,1)\to(-1,1). Follow the point (1,2)(1,2) to see the difference: the first order takes it to (−1,1)(-1,1), the second to (−2,3)(-2,3).

2. Swapping columns and rows. With A=[1234]\mathbf{A}=\begin{bmatrix}1&2\\3&4\end{bmatrix} and B=[0110]\mathbf{B}=\begin{bmatrix}0&1\\1&0\end{bmatrix}, AB=[2143]\mathbf{A}\mathbf{B}=\begin{bmatrix}2&1\\4&3\end{bmatrix} swaps the columns of A\mathbf{A} and BA=[3412]\mathbf{B}\mathbf{A}=\begin{bmatrix}3&4\\1&2\end{bmatrix} swaps its rows. The column-times-row sum above gives the same AB\mathbf{A}\mathbf{B}.

3. Shapes. Now take a new 2×32\times3 matrix A\mathbf{A} and a new 3×23\times2 matrix B\mathbf{B}:

A=[102011],B=[120110].\mathbf{A}=\begin{bmatrix}1&0&2\\0&1&1\end{bmatrix},\qquad \mathbf{B}=\begin{bmatrix}1&2\\0&1\\1&0\end{bmatrix}.

AB\mathbf{A}\mathbf{B} is 2×22\times2: the first entry is 1⋅1+0⋅0+2⋅1=31\cdot1+0\cdot0+2\cdot1=3, and in all

AB=[3211],BA=[124011102].\begin{aligned} \mathbf{A}\mathbf{B}&=\begin{bmatrix}3&2\\1&1\end{bmatrix},\\ \mathbf{B}\mathbf{A}&=\begin{bmatrix}1&2&4\\0&1&1\\1&0&2\end{bmatrix}. \end{aligned}

BA\mathbf{B}\mathbf{A} is 3×33\times3. Here the two orders don’t even have the same shape.

Predict before you compute: a 4×34\times3 matrix times a 3×53\times5 matrix gives what shape? 4×54\times5: 20 entries, each one row of 3 numbers dotted with one column of 3.

Where you’ll meet this

Graphics. A drawing program that scales a shape, turns it and tilts it multiplies the three matrices once and applies the single product to every corner of the shape. One product, thousands of corners. To move shapes too, graphics programs add one extra coordinate so that a move becomes a matrix as well.

Layers of a neural network. Two layers that take 784 numbers to 128 and then 128 to 10 hold a 128×784128\times784 and a 10×12810\times128 matrix: 101,632 numbers. If nothing happened between the layers, their product would be a single 10×78410\times784 matrix of 7,840 numbers doing the same job. Real networks put a step that is not linear between the layers, and that step is what stops the two from merging into one.

Filters in a chain. In signal processing a filter is a matrix too, so two filters in a chain are a product of two matrices.

In signal processing · cascaded filters

Discrete convolution, in the signal processing course, shows that two filters in a chain give the same output in either order. That is a special property of filters, which are linear and time-invariant. Matrices in general do not have it, as the turn and the shear on this page show.

Reference card

QuantityFormulaNotes
Product as a map(AB)x=A(Bx)(\mathbf{A}\mathbf{B})\mathbf{x}=\mathbf{A}(\mathbf{B}\mathbf{x})do B\mathbf{B} first, then A\mathbf{A}
By columnscolumn qq of AB\mathbf{A}\mathbf{B} is Abq\mathbf{A}\mathbf{b}_qA\mathbf{A} times each column of B\mathbf{B}
By entries(AB)pq=∑k=1napkbkq(\mathbf{A}\mathbf{B})_{pq}=\sum_{k=1}^{n}a_{pk}b_{kq}row pp of A\mathbf{A} dotted with column qq of B\mathbf{B}
By piecesAB=∑k=1n(column k)(row k)\mathbf{A}\mathbf{B}=\sum_{k=1}^{n}(\text{column } k)(\text{row } k)column kk of A\mathbf{A}, row kk of B\mathbf{B}
Shapes(m×n)(n×K)=m×K(m\times n)(n\times K)=m\times Kthe inside numbers must match
Grouping(AB)C=A(BC)(\mathbf{A}\mathbf{B})\mathbf{C}=\mathbf{A}(\mathbf{B}\mathbf{C})the same chain of maps
OrderAB≠BA\mathbf{A}\mathbf{B}\ne\mathbf{B}\mathbf{A} in generalturn then shear differs from shear then turn
IdentityAI=IA=A\mathbf{A}\mathbf{I}=\mathbf{I}\mathbf{A}=\mathbf{A}1s on the diagonal, 0s elsewhere

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look