Matrices and matrix multiplication, explained with pictures
A matrix is numbers laid out in a rectangle. If you read those numbers as a plan for changing the plane, multiplication starts to make sense. Drag the arrows and press the buttons.
Short answer. A matrix is numbers laid out in a rectangle, and you can read it as a plan for changing the plane. Matrix times vector moves one arrow by that plan. Matrix times matrix joins two changes into one. That is why order matters. Usually AB ≠ BA.
Left column = where î goes, right column = where ĵ goes
v = ( , )
Drag the circles at the arrow tips, or change the numbers. The purple arrow v is before the move. The black arrow M v is after it. The dashed path goes "x times where î went, then y times where ĵ went."
Definitions
The page below uses 2×2 matrices, but the definitions are the same for any size. The entries are real numbers.
- matrix-vector product
- Let the columns of an m×n matrix A be , and let x be a column vector with n entries. Then Ax is the vector with m entries made by the linear combination of the columns of A with the entries of x as weights. If the number of columns of A is not the number of entries of x, it is not defined.
- matrix product
- If is m×n and is n×p, the product AB is the m×p matrix whose (i, j) entry is given below. If the number of columns of A is not the number of rows of B, it is not defined. With this definition, for every x.
- linear transformation
- A function is a linear transformation if it meets both conditions below for all vectors u, v and every real number c. For an m×n matrix A, is a linear transformation.
- standard matrix
- Let be the vector with 1 in entry j and 0 everywhere else. For a linear transformation , there is exactly one m×n matrix A with T(x) = Ax for every x, and its column j is . This A is the standard matrix of T. In the plane, and .
1. A matrix is numbers laid out in a rectangle
A matrix is numbers laid out in rows (going across) and columns (going down).
This matrix has 2 rows (1 2 3 and 4 5 6) and 3 columns (1 4, 2 5, 3 6). So it is called a 2×3 matrix. The size is always given as rows × columns, rows first.
You point to one entry by its row and column. In the matrix above, row 1, column 3 is 3. Books write this as a₁₃ = 3, with the row number first and the column number second, in small type.
On this page, when a matrix is written on one line, it looks like [1 2 / 3 4]. The part before / is the top row. The part after it is the bottom row.
Reading it as a plan
You can read a 2×2 matrix as a plan for changing the plane. The plane has two base arrows: î = (1, 0), one square to the right, and ĵ = (0, 1), one square up. A 2×2 matrix writes down where to send these two. The left column is where î goes. The right column is where ĵ goes.
The starting matrix in the lab, M = [1 −1 / 1 1], sends î to (1, 1) and ĵ to (−1, 1). When the two arrows move, the whole grid follows. The faint green and blue grid is the grid after the change.
2. Matrix times vector: moving one arrow
Multiply a matrix by a vector and you get a new vector. It is one arrow moved by the plan. Here is how to work it out.
In formulas, (x, y) is written standing up as a column. It is the same vector.
[a b / c d]: a 2×2 matrix. a b before the/is the top row, and c d after it is the bottom row.(x, y): the vector v being multiplied: go x times along î, then y times along ĵ.(a×x + b×y, c×x + d×y): the moved vector. The first number is the new across value, the second the new up value.
You do it once per row. Pair the top row (a, b) with (x, y), multiply each pair, and add. That gives the new across value. Do the same with the bottom row (c, d) to get the new up value. This "pair up, multiply, add" step is the dot product from the vectors page.
You can also read the same sum another way. v = (x, y) means "go x times along î, then y times along ĵ." The matrix moves î and ĵ, so M v means go x times along where î went, then y times along where ĵ went.
M: the matrix[a b / c d]from above.v: the vector (x, y).(a, c): the left column of M, where î went.(b, d): the right column, where ĵ went.x,y: the two numbers of v, how many times to go along each column.
The dashed lines in the lab show this path. With the starting numbers, [1 −1 / 1 1] × (2, 1) is 1×2 + (−1)×1 = 1 for the top row and 1×2 + 1×1 = 3 for the bottom row, so the answer is (1, 3). Reading by columns gives the same thing: 2×(1, 1) + 1×(−1, 1) = (2, 2) + (−1, 1) = (1, 3).
The sizes must fit. The number of columns in the matrix must equal the number of entries in the vector. A 2×2 matrix takes a vector with 2 entries. A 2×3 matrix takes a vector with 3 entries and gives back a vector with 2 entries.
Try this. ① Move v to (1, 0). M v becomes (1, 1) and lands on top of where î went. You get the left column of the matrix. ② Put v at (0, 0). M v is (0, 0) too. A matrix never moves the origin. ③ Drag the tip of ĵ to (−1, −1) so it sits on the same line as î (M = [1 −1 / 1 −1]). Now M v lands on that line wherever you put v. The starting v (2, 1) goes to (1, 1). The plane has been squashed onto one line.
3. Matrix times matrix: two changes in one
Say you change the plane with B, then change it again with A. There is one matrix that does both changes at once. That matrix is the product AB. Pick A and B below and press a button. The plane slowly changes to the first matrix, then on to the second.
The dashed square is one grid square before any change. In the second step, the faint dashed F shows the plane after the first matrix only. The solid F is where things are now.
The one on the right goes first
AB applies B first, then A. When you multiply a vector v, the matrix next to v, which is B, does its work first.
A,B: matrices that change the plane (plans).AB: the one matrix that does B, then A, in a single step.v: the vector being moved.B vis v moved by B first, andA (B v)is that result moved again by A.
B moves v first, then A moves the result. It is like f(g(x)) for functions, where g runs first. So the order you write them in (left to right) is the reverse of the order they act in.
How to work it out: pair a row with a column
Each entry of AB comes from pairing that row of A with that column of B, multiplying each pair, and adding. For row 1, column 2, use row 1 of A and column 2 of B.
[a b / c d]: the first matrix, A.[e f / g h]: the second matrix, B. The part before/is the top row, the part after it the bottom row.- The entries on the right (
a×e + b×gand so on): the entries of AB. Each pairs that row of A with that column of B, multiplies, and adds.a×e + b×guses row 1 of A, (a, b), and column 1 of B, (e, g).
Why this rule? The left column of AB is "where î ends up." B sends î to its left column (e, g). Then A moves that to A × (e, g) = (a×e + b×g, c×e + d×g). It is the matrix times vector sum from section 2, done once for each column of B. To watch this rule being built one step at a time, see 4. How the formula is built.
In the lab, point at a cell of the product or move to it with Tab. The row and column it uses get an orange border, and the sum appears. With the starting numbers, row 2, column 1 of AB is (1, 0) · (2, 0) = 1×2 + 0×0 = 2.
When you can multiply
To multiply two matrices, the number of columns in the first must equal the number of rows in the second. Each pair needs a partner, so the counts must match. The result has the rows of the first × the columns of the second.
| first × second | Can you multiply? | Result size |
|---|---|---|
| 2×2 × 2×2 | Yes (2 = 2) | 2×2 |
| 2×3 × 3×4 | Yes (3 = 3) | 2×4 |
| 3×1 × 1×3 | Yes (1 = 1) | 3×3 |
| 2×3 × 2×3 | No (3 ≠ 2) | none |
4. How the formula is built: why "row · column"
The rule in section 3 looks like something to memorize, but it follows straight from two things you have already seen. First, the left column of a matrix is where î goes, and the right column is where ĵ goes (section 1). Second, AB applies B first, then A (section 3). Press "Next" and watch î and ĵ move once with B and again with A, while the formula under the picture grows one line at a time.
Entries are named with a small row number and column number, as in section 1. The entries of A are a₁₁ a₁₂ / a₂₁ a₂₂, and the entries of B are b₁₁ b₁₂ / b₂₁ b₂₂. Two names written side by side, like a₁₁b₁₁, are multiplied. The numbers here are A = [1 −1 / 1 1] and B = [2 1 / 1 2]. A is the starting M from the first lab. No entry is 0, so none of the multiply-and-add pieces disappears.
In the formula under the picture, the line with the orange background was added in this step, and the faint text is the same line worked out with this page's numbers. In the matrices on the right, an orange border marks the entries used in this step. The faint green and blue grid is the plane changed so far, the dashed square is one grid square before any change, and the gray dashed arrows show where things were after B only.
The steps above, written only as formulas. The last line uses the numbers on this page.
- The goal. The left column of AB is where AB sends î, which is
(AB) î. - B goes first.
(AB) î = A (B î). B sends î to its own left column(b₁₁, b₂₁). Here that is (2, 1). - Then A. Reading by columns, as in section 2:
A (b₁₁, b₂₁) = b₁₁ (a₁₁, a₂₁) + b₂₁ (a₁₂, a₂₂). Go b₁₁ times along the left column of A, then b₂₁ times along its right column. Here that is2×(1, 1) + 1×(−1, 1) = (1, 3). B î = (2, 1) happens to be the starting v of the first lab, so this is the same sum as M v = (1, 3) in section 2. - Split into entries. Add the two arrows entry by entry:
(a₁₁b₁₁ + a₁₂b₂₁, a₂₁b₁₁ + a₂₂b₂₁). The top entry pairs row 1 of A with column 1 of B. The bottom entry pairs row 2 of A with column 1 of B. Here they are1×2 + (−1)×1 = 1and1×2 + 1×1 = 3. - ĵ takes the same path. B sends ĵ to its own right column
(b₁₂, b₂₂), and A moves it again. That gives the right column of AB,(a₁₁b₁₂ + a₁₂b₂₂, a₂₁b₁₂ + a₂₂b₂₂). Here ĵ goes through (1, 2) and lands on (−1, 3). - Done. Written as one formula for all four entries:
(AB)ᵢⱼ = aᵢ₁b₁ⱼ + aᵢ₂b₂ⱼ = Σₖ aᵢₖbₖⱼ. The entry in row i, column j pairs row i of A with column j of B, multiplies, and adds. Σₖ means "add these up as k goes 1, 2." With i = j = 1, this isa×e + b×gfrom section 3.
Bigger sizes work the same way. If A is m×n and B is n×p, there are p base arrows instead of 2, but each one still goes to its column of B and then gets moved by A. So (AB)ᵢⱼ = aᵢ₁b₁ⱼ + aᵢ₂b₂ⱼ + … + aᵢₙbₙⱼ, with k going from 1 to n. Here n is both the number of columns in A and the number of rows in B. That is why the two must match, as in "When you can multiply" in section 3. The picture only shows the 2×2 case.
In code, Σ is a single for loop.
// row i, column j of AB (counting from 0)
let sum = 0;
for (let k = 0; k < n; k++) {
sum += A[i][k] * B[k][j];
}
Try this. ① At step ④, the left column of AB fills in as (1, 3). The orange dashed lines run from the arrow tip down to the across axis and over to the up axis. They show that the top entry 1 is the tip's across value and the bottom entry 3 is its up value. ② At the last step, AB = [1 −1 / 3 3]. The −1 in row 1, column 2 comes from row 1 of A, (1, −1), and column 2 of B, (1, 2): 1×1 + (−1)×2 = −1. If you mix them up and use row 2 of A with column 1 of B, you get 3, which is wrong. ③ In the lab in section 3, type 1, −1, 1, 1 into the A boxes and 2, 1, 1, 2 into the B boxes, then press "B first, then A (AB)". You get the same [1 −1 / 3 3]. "A first, then B (BA)" gives [3 −1 / 3 1].
5. Order matters: AB ≠ BA
With numbers, 2×3 = 3×2. With matrices, that is usually not true. Look at the starting numbers in the lab. A turns the plane 90 degrees to the left. B doubles it across.
AB turns an F that was already stretched across. The long side stands up, so the F ends up tall. BA stretches an F that was already turned. So the F ends up wide. Same two moves, different order, different result.
Sometimes the order does not matter. That happens when one of them is the matrix that changes nothing, when both are the same matrix, or when both only stretch across and up separately. These are the exceptions.
Try this. ① With the starting numbers, press both buttons in turn. AB = [0 −1 / 2 0] and BA = [0 −2 / 1 0]. They are different. ② Set A to "No change." Whichever button you press, the product equals B. ③ Set both A and B to "Turn 90°." AB = BA = [−1 0 / 0 −1]. Two quarter turns make a half turn. ④ Set A to "Double across" and B to "Flip." AB = BA = [−2 0 / 0 1]. Both only touch the across direction, so the order does not matter.
6. Rules worth knowing
| Rule | Formula | In plain words |
|---|---|---|
| Swapping the order usually changes the result | AB ≠ BA | The order of the changes changes the result. When they match, it is an exception |
| Grouping does not matter | (AB)C = A(BC) | With three changes, keep the order C → B → A, and you can join any two of them first |
| The identity matrix changes nothing | IA = AI = A | I = [1 0 / 0 1] leaves everything as it is, like the number 1 |
| You can split it over a sum | A(B + C) = AB + AC | Like opening brackets. To add matrices, add the numbers in the same spots |
| Area factors multiply | det(AB) = det(A) × det(B) | Make area 2 times bigger, then 3 times bigger, and it is 6 times bigger. See the determinant page |
Because grouping does not matter, you can multiply many changes together ahead of time and keep just one matrix. Even with tens of thousands of points, you only multiply by that one matrix.
7. Work one out by hand
Let A = [1 2 / 3 4] and B = [5 6 / 7 8]. Find AB.
- Check the sizes. A and B are both 2×2. A has 2 columns and B has 2 rows, so you can multiply, and the result is 2×2.
- Row 1, column 1. Row 1 of A is (1, 2). Column 1 of B is (5, 7).
1×5 + 2×7 = 5 + 14 = 19 - Row 1, column 2. Row 1 of A is (1, 2). Column 2 of B is (6, 8).
1×6 + 2×8 = 6 + 16 = 22 - Row 2, column 1. Row 2 of A is (3, 4). Column 1 of B is (5, 7).
3×5 + 4×7 = 15 + 28 = 43 - Row 2, column 2. Row 2 of A is (3, 4). Column 2 of B is (6, 8).
3×6 + 4×8 = 18 + 32 = 50 - Put them together.
AB = [19 22 / 43 50]
The same work written as formulas:
Check your answer. Try (1, 0). B sends (1, 0) to (5, 7). A sends (5, 7) to (1×5 + 2×7, 3×5 + 4×7) = (19, 43). That matches the left column of AB, (19, 43).
The other order, BA = [23 34 / 31 46], is different. Type these numbers into the A and B boxes in the lab to check. The picture goes off the screen, but the numbers still show.
Where is it used?
- 3D graphics and games. Scaling, turning and moving an object are each written as a matrix, then multiplied into one. Get the order wrong and the object ends up in the wrong place. Turning in place and then moving is not the same as moving and then turning around the origin. In 3D, 4×4 matrices are used so that moving can also be done by one multiplication.
- Neural networks. One layer of a neural network is
W × x + b. Multiply the input vector x by the weight matrix W, add the bias b, then apply a bending function (the activation function). If the input has 784 entries and the next layer has 128, W is a 128×784 matrix. Put several inputs side by side as columns, and one matrix times matrix handles them all. Without the bending function, stacking many layers is no better than one layer, because the multiplied matrices join into a single matrix. - Systems of equations.
x + 2y = 5and3x + 4y = 11can be written as[1 2 / 3 4] × (x, y) = (5, 11). For short,Ax = b(A is the matrix of numbers, x is the unknowns (x, y), b is the right-hand side (5, 11)). The answer is x = 1, y = 2. If A has a matrix that undoes it, A⁻¹, you can find the answer asx = A⁻¹b.
What to read next: how much a matrix scales area is on the determinant page, and the matrix that undoes a change is on the inverse matrix page. To undo two changes, undo the later one first: (AB)⁻¹ = B⁻¹A⁻¹. If you put on socks and then shoes, you take off the shoes first.
Common questions
What is a matrix?
It is numbers laid out in a rectangle of rows and columns. A 2×2 matrix can be read as a plan, written in four numbers, for stretching, turning or leaning the plane.
Why doesn't matrix multiplication just multiply the numbers in the same spots?
Matrix multiplication works out one change that does two changes at once. That is why it pairs a row of the first matrix with a column of the second, multiplies, and adds. Multiplying the numbers in the same spots is a different operation, called the Hadamard product.
Why are AB and BA different?
Because the changes happen in a different order. AB applies B first, then A. Stretching sideways by 2 and then turning 90 degrees gives a different result from turning 90 degrees and then stretching sideways by 2.
Can you multiply matrices of different sizes?
Yes, if the number of columns in the first matrix equals the number of rows in the second. A 2×3 matrix times a 3×4 matrix gives a 2×4 matrix. A 2×3 matrix times a 2×3 matrix cannot be done.
What is the identity matrix?
It is the matrix with 1 on the diagonal from top left to bottom right and 0 everywhere else. It changes nothing, so multiplying any matrix of the right size by it gives the same matrix back. It does the job that the number 1 does for numbers.
Words
| Word used here | Word used in textbooks |
|---|---|
| numbers laid out in a rectangle | matrix |
| row, column | row, column |
| size 2×3 | a 2×3 matrix, read "2 by 3" |
| one number in the matrix | entry, element. Written like a₁₂ |
| changing the plane | linear transformation |
| base arrows î, ĵ | basis vectors |
| joining two changes into one | composition |
| the matrix that changes nothing | identity matrix. Written I |
| order does not matter | commutative law. Usually false for matrix multiplication |
| grouping does not matter | associative law |
| splitting over a sum | distributive law |
| multiplying the numbers in the same spots | Hadamard product, element-wise product |
| the matrix that undoes a change | inverse matrix. Written A⁻¹ |