math.wiki
Linear algebra › Matrices › Matrix multiplication
Open on the map
Precalculus · N.VM.C.8 · N.VM.C.10

Matrix multiplication

One table says how many of each flower goes into each arrangement, and another says what each flower costs at each supplier. How do you combine the two into one table of totals, and what kind of multiplication does that turn out to be?

Where you are 12 in Matrices

Why this exists

Reiko runs a flower stall and makes two table centerpieces. A small one takes 3 tulips, 5 lilies and 2 carnations; a large one takes 7 tulips, 9 lilies and 3 carnations. Two suppliers charge different prices per stem. To cost a small centerpiece from the first supplier, multiply each stem count by that supplier’s price for the flower, and add:

3(1.20) + 5(2.50) + 2(0.90) = 17.90

That is one of four totals, all of the same shape: walk along a recipe, walk down a price list, multiply matching items, add. Matrix multiplication does this for every recipe and price list at once. It is not entry by entry, as adding matrices is: it pairs a row of the first table with a column of the second.

Definition

In plain words: the entry in row i, column j of the product AB comes from row i of A and column j of B. Multiply first entry by first entry, second by second, and so on to the end, and add the products.

In symbols, let A = [a_{ij}] be m \times n and B = [b_{ij}] be n \times p (sizes and entry names as in Rows, columns and dimensions). The product AB is the m \times p matrix with entries

\begin{aligned} (AB)_{ij} &= a_{i1}b_{1j} + a_{i2}b_{2j} + \cdots + a_{in}b_{nj} \\ &= \sum_{k=1}^{n} a_{ik}\,b_{kj} \end{aligned}

When the product exists. AB is defined only when the number of columns of A equals the number of rows of B. Written side by side, the inner sizes must match, and the outer sizes give the size of the answer:

(m \times n)(n \times p) \;\longrightarrow\; m \times p

Otherwise AB is undefined. A square matrix can be multiplied by itself, and A^2 means AA.

The identity matrix. The n \times n identity matrix I_n has 1 in every position on the main diagonal (row 1, column 1 down to row n, column n) and 0 everywhere else:

I_2 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}
I_3 = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}

Intuition

3 −1 2 4 0 −2 1 5 −3 2 0 4 = 6 21 4 12
A\;(2 \times 3)
B\;(3 \times 2)
AB\;(2 \times 2)
Figure 1Row 2 of A meets column 2 of B to give the entry in row 2, column 2 of AB, which is 4(5) + 0(2) + (-2)(4) = 12.

A row of an m \times n matrix has n entries, one per column. A column of an n \times p matrix has n entries, one per row. The rule pairs them off, first with first, and every entry needs a partner. A row of 3 against a column of 4 would leave one entry of the column with nothing to multiply. That is why the inner sizes must match.

At the flower stall, recipes and price lists are both listed by flower. The flower is the index that gets added up and disappears; centerpieces name the rows of the product and suppliers its columns. So order matters: in AB the rows come from A, and BA asks a different question, often a meaningless or impossible one.

Results

For matrices whose sizes make each sum and product defined, and any scalar k:

  • Size rule. An m \times n matrix times an n \times p matrix is m \times p. Otherwise the product is undefined.
  • Not commutative. In general AB \ne BA. BA may be undefined while AB exists, it may have another size, or it may have the same size and different entries. Two matrices with AB = BA are said to commute.
  • Identity. For an m \times n matrix A, \;I_m A = A\; and \;A I_n = A.
  • Associative. (AB)C = A(BC), so a product such as ABC needs no brackets.
  • Distributive. A(B + C) = AB + AC and (A + B)C = AC + BC.
  • Scalars. k(AB) = (kA)B = A(kB).
Why it is true

The size rule is the definition read aloud. Non-commutativity needs only one pair with AB \ne BA, and Example 4 gives one.

For the identity, write \delta_{ik} for the entry of I_m in row i, column k: it is 1 when k = i and 0 otherwise. Then

(I_m A)_{ij} = \sum_{k=1}^{m} \delta_{ik}\,a_{kj} = a_{ij}

because every term except k = i is multiplied by 0. So I_m A = A, and A I_n = A by the same argument with columns.

For associativity, let A be m \times n, B be n \times p and C be p \times q. The entry in row i, column j of (AB)C is

\sum_{l=1}^{p} (AB)_{il}\,c_{lj} = \sum_{l=1}^{p} \Big(\sum_{k=1}^{n} a_{ik}b_{kl}\Big) c_{lj}

Distributing c_{lj} gives one big sum with a term a_{ik}b_{kl}c_{lj} for every pair (k, l). The entry of A(BC) is \sum_k a_{ik}\big(\sum_l b_{kl}c_{lj}\big), and distributing a_{ik} gives the same terms. Sums of the same finitely many numbers are equal, whatever the order and grouping, by the commutative and associative properties of addition. The distributive law comes the same way from the distributive property of numbers.

Worked examples

Example 1

Row by column

Find AB, where

A = \begin{bmatrix} 3 & -1 & 2 \\ 4 & 0 & -2 \end{bmatrix} \qquad B = \begin{bmatrix} 1 & 5 \\ -3 & 2 \\ 0 & 4 \end{bmatrix}
Solution

A is 2 \times 3 and B is 3 \times 2. The inner sizes are both 3, so AB is defined, and it is 2 \times 2. Each entry is a row of A times a column of B:

\begin{aligned} (AB)_{11} &= 3(1) + (-1)(-3) + 2(0) = 6 \\ (AB)_{12} &= 3(5) + (-1)(2) + 2(4) = 21 \\ (AB)_{21} &= 4(1) + 0(-3) + (-2)(0) = 4 \\ (AB)_{22} &= 4(5) + 0(2) + (-2)(4) = 12 \end{aligned}
AB = \begin{bmatrix} 6 & 21 \\ 4 & 12 \end{bmatrix}

Figure 1 shows the last entry. BA also exists, but it is 3 \times 3: not even the size of AB.

Example 2

Costing the centerpieces

Reiko’s recipe matrix Q has rows small, large and columns tulips, lilies, carnations:

Q = \begin{bmatrix} 3 & 5 & 2 \\ 7 & 9 & 3 \end{bmatrix}

Her price matrix P, in dollars per stem, has rows tulips, lilies, carnations and columns supplier 1, supplier 2:

P = \begin{bmatrix} 1.20 & 1.40 \\ 2.50 & 2.25 \\ 0.90 & 0.80 \end{bmatrix}

Find QP and say what its entries mean. Minoru orders 10 small and 4 large centerpieces for a café opening. Which supplier should Reiko use, and how much does that save?

Solution

Q is 2 \times 3 and P is 3 \times 2; the shared 3 is the three flowers. QP is 2 \times 2, rows for centerpieces and columns for suppliers. Row 1 of Q times column 1 of P is the 17.90 from the start of the lesson. The others go the same way, for example 7(1.40) + 9(2.25) + 3(0.80) = 32.45:

QP = \begin{bmatrix} 17.90 & 17.05 \\ 33.60 & 32.45 \end{bmatrix}

Row 2, column 1 says a large centerpiece costs 33.60 dollars with supplier 1. Supplier 2 is cheaper for both sizes. Write the order as the row matrix \begin{bmatrix} 10 & 4 \end{bmatrix}; times QP it gives a 1 \times 2 matrix, with first entry 10(17.90) + 4(33.60) = 313.40 and its second 10(17.05) + 4(32.45) = 300.30:

\begin{bmatrix} 10 & 4 \end{bmatrix} QP = \begin{bmatrix} 313.40 & 300.30 \end{bmatrix}

Supplier 2 saves 13.10 dollars. PQ is also defined, but it would pair suppliers with centerpiece sizes and add up nothing real: the order of the factors carries the meaning.

Example 3

Which products exist

A is 2 \times 3, B is 3 \times 4, C is 4 \times 2 and D is 2 \times 2. For each of AB, BA, AC, CA, BC, DA, AD and ABC, decide whether it is defined, and if so give its size.

Solution

Only the sizes matter; compare the inner pair:

ProductSizesResult
AB(2 \times 3)(3 \times 4)2 \times 4
BA(3 \times 4)(2 \times 3)undefined, 4 \ne 2
AC(2 \times 3)(4 \times 2)undefined, 3 \ne 4
CA(4 \times 2)(2 \times 3)4 \times 3
BC(3 \times 4)(4 \times 2)3 \times 2
DA(2 \times 2)(2 \times 3)2 \times 3
AD(2 \times 3)(2 \times 2)undefined, 3 \ne 2

For ABC, AB is 2 \times 4 and then (2 \times 4)(4 \times 2) is 2 \times 2.

Example 4

Same size, different answer

Find AB and BA, where

A = \begin{bmatrix} 3 & 1 \\ -2 & 4 \end{bmatrix} \qquad B = \begin{bmatrix} 0 & 5 \\ 1 & -1 \end{bmatrix}
Solution

Both products are 2 \times 2. For example (AB)_{22} = -2(5) + 4(-1) = -14, a row of A with a column of B, while (BA)_{11} = 0(3) + 5(-2) = -10, a row of B with a column of A. In full,

AB = \begin{bmatrix} 1 & 14 \\ 4 & -14 \end{bmatrix}
BA = \begin{bmatrix} -10 & 20 \\ 5 & -3 \end{bmatrix}

Not one entry agrees, so multiplication is not commutative. Some pairs do commute, such as any square matrix with itself.

Example 5

The identity on both sides

Let M = \begin{bmatrix} 5 & -3 & 1 \\ 2 & 7 & -4 \end{bmatrix}. Find I_2 M and M I_3. Is I_3 M defined?

Solution

I_2 M is (2 \times 2)(2 \times 3), so 2 \times 3. Row 1 of I_2 is \begin{bmatrix} 1 & 0 \end{bmatrix}, so it picks the top entry of each column of M; for example (I_2 M)_{12} = 1(-3) + 0(7) = -3. Row 2 picks the bottom entry. So I_2 M = M.

M I_3 is (2 \times 3)(3 \times 3). Column j of I_3 has its 1 in position j, so it picks entry j of each row of M; for example (M I_3)_{23} is 2(0) + 7(0) + (-4)(1) = -4. So M I_3 = M.

I_3 M is (3 \times 3)(2 \times 3), and 3 \ne 2, so it is undefined. For an n \times n matrix both identities are I_n, and I_n A = A I_n = A: the identity does for matrices what 1 does for numbers.

Example 6

Checking associativity

Let

A = \begin{bmatrix} 1 & 2 \\ -1 & 0 \end{bmatrix} \qquad B = \begin{bmatrix} 3 & 1 \\ 0 & 2 \end{bmatrix}
C = \begin{bmatrix} 2 \\ -1 \end{bmatrix}

Find (AB)C and A(BC).

Solution

The sizes are 2 \times 2, 2 \times 2 and 2 \times 1, so both groupings give a 2 \times 1 matrix. Grouping on the left, AB = \begin{bmatrix} 3 & 5 \\ -3 & -1 \end{bmatrix} and

(AB)C = \begin{bmatrix} 3(2) + 5(-1) \\ -3(2) + (-1)(-1) \end{bmatrix} = \begin{bmatrix} 1 \\ -5 \end{bmatrix}

Grouping on the right, BC = \begin{bmatrix} 5 \\ -2 \end{bmatrix} and

A(BC) = \begin{bmatrix} 1(5) + 2(-2) \\ -1(5) + 0(-2) \end{bmatrix} = \begin{bmatrix} 1 \\ -5 \end{bmatrix}

The two agree, as the associative property says. The work differs (12 multiplications on the left, 8 on the right), but never the answer.

Example 7

Which matrices commute with this one?

Let A = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}. Find every 2 \times 2 matrix B = \begin{bmatrix} a & b \\ c & d \end{bmatrix} with AB = BA.

Solution

Multiply both ways:

AB = \begin{bmatrix} a + c & b + d \\ c & d \end{bmatrix}
BA = \begin{bmatrix} a & a + b \\ c & c + d \end{bmatrix}

Compare positions. Top left: a + c = a, so c = 0. Top right: b + d = a + b, so d = a. Bottom left: c = c, no condition. Bottom right: d = c + d, so c = 0 again. So

B = \begin{bmatrix} a & b \\ 0 & a \end{bmatrix}

for any numbers a and b, which includes I_2 and A itself. A matrix with c \ne 0 fails: for B = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}, AB = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} but BA = \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix}.

Boundaries

Columns of the first, rows of the second. AB exists only when A has as many columns as B has rows. A^2 exists only for a square A.

The order of factors is fixed. AB and BA differ in general, so factors cannot be swapped. In particular (A + B)^2 = A^2 + AB + BA + B^2, which is A^2 + 2AB + B^2 only when A and B commute.

A zero product does not need a zero factor. AB can be the zero matrix while neither factor is (Exercise 15), and AB = AC does not force B = C. Canceling, as with numbers, is not allowed.

The identity has a size. I_m A = A = A I_n for an m \times n matrix A.

Where this goes next

Writing a system as a matrix equation turns a whole linear system into one product AX = B: each row of coefficients times the column of variables is the left side of one equation. Read column by column, the same product is a combination of the columns.

Because I plays the part of 1, one can ask for a matrix whose product with A is I: the inverse, which exists when the determinant is not zero. The transpose reverses the order of a product. And doing one transformation of the plane after another is multiplying their matrices, where order matters just as in composing functions.

Exercises

Find each product

  1. \begin{bmatrix} 1 & 4 \\ 2 & -3 \end{bmatrix}\begin{bmatrix} 5 & 0 \\ -1 & 2 \end{bmatrix}
  2. \begin{bmatrix} 2 & 0 & -1 \end{bmatrix}\begin{bmatrix} 3 \\ 5 \\ 4 \end{bmatrix}
  3. \begin{bmatrix} 3 \\ 5 \\ 4 \end{bmatrix}\begin{bmatrix} 2 & 0 & -1 \end{bmatrix}
  4. \begin{bmatrix} 1 & -2 & 0 \\ 3 & 1 & 2 \end{bmatrix}\begin{bmatrix} 2 & 1 \\ 0 & -1 \\ 4 & 3 \end{bmatrix}
  5. \begin{bmatrix} 2 & 1 \\ -1 & 3 \\ 0 & 4 \end{bmatrix}\begin{bmatrix} 1 & 0 & 2 \\ -2 & 5 & 1 \end{bmatrix}
  6. \displaystyle \begin{bmatrix} \frac{1}{2} & 1 \\ 0 & -\frac{1}{3} \end{bmatrix}\begin{bmatrix} 4 & 6 \\ 3 & -9 \end{bmatrix}

When the product exists

A is 3 \times 2, B is 2 \times 4 and C is 4 \times 4. Say whether each product is defined, and if it is, give its size.

  1. AB
  2. BA
  3. BC and CB
  4. A^2 and C^2

Order, identity and grouping

  1. Let P = \begin{bmatrix} 2 & 1 \\ 0 & -1 \end{bmatrix} and Q = \begin{bmatrix} 1 & 3 \\ 4 & 0 \end{bmatrix}. Find PQ and QP.
  2. With P from Exercise 11 and R = \begin{bmatrix} 3 & 0 \\ 0 & 3 \end{bmatrix}, find RP and PR. Explain the result.
  3. Let A = \begin{bmatrix} 1 & -1 \\ 2 & 0 \end{bmatrix}, B = \begin{bmatrix} 0 & 3 \\ 1 & 1 \end{bmatrix} and C = \begin{bmatrix} 2 & 1 \\ -1 & 4 \end{bmatrix}. Find (AB)C and A(BC).
  4. Let A = \begin{bmatrix} 2 & -1 \\ 1 & 0 \end{bmatrix}. Find A^2 and A^3.
  5. Let A = \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix} and B = \begin{bmatrix} 2 & -4 \\ -1 & 2 \end{bmatrix}. Find AB and BA.

In context

  1. Naoki makes tall and short vases of flowers. N = \begin{bmatrix} 6 & 10 \\ 4 & 5 \end{bmatrix} has rows tall, short and columns lilies, carnations (stems per vase). Lilies cost 2.75 dollars a stem and carnations 0.80 dollars. Find the cost of each vase as a matrix product.
  2. Minoru orders 12 tall and 5 short vases from Naoki. Use Exercise 16 and one more product to find the cost of the order.
  3. In one day Minoru, Naoki and Reiko (rows) spend these hours arranging and delivering (columns): H = \begin{bmatrix} 6 & 2 \\ 3 & 5 \\ 4 & 4 \end{bmatrix}. Pay rates in dollars per hour, rows arranging, delivering and columns weekday, weekend, are R = \begin{bmatrix} 18 & 24 \\ 15 & 20 \end{bmatrix}. Find HR, say what its row 3, column 2 entry means, and say whether RH is defined.

Reasoning

  1. Explain, in terms of a row and a column, why AB is undefined when A has 3 columns and B has 4 rows.
  2. Show that (A + B)^2 = A^2 + AB + BA + B^2 for square matrices of one size, naming the property used. When does it equal A^2 + 2AB + B^2?
  3. Prove that I_m A = A for every m \times n matrix A, using the entry formula.
  4. Let D = \begin{bmatrix} 2 & 0 \\ 0 & 3 \end{bmatrix}. Find every 2 \times 2 matrix that commutes with D.
Answers
  1. \begin{bmatrix} 1 & 8 \\ 13 & -6 \end{bmatrix}. The top left is 1(5) + 4(-1) = 1.
  2. \begin{bmatrix} 2 \end{bmatrix}, a 1 \times 1 matrix: 2(3) + 0(5) + (-1)(4) = 2.
  3. \begin{bmatrix} 6 & 0 & -3 \\ 10 & 0 & -5 \\ 8 & 0 & -4 \end{bmatrix}. A (3 \times 1)(1 \times 3) product is 3 \times 3, each entry a single product.
  4. \begin{bmatrix} 2 & 3 \\ 14 & 8 \end{bmatrix}. The bottom left is 3(2) + 1(0) + 2(4) = 14.
  5. \begin{bmatrix} 0 & 5 & 5 \\ -7 & 15 & 1 \\ -8 & 20 & 4 \end{bmatrix}. Row 2, column 1 is -1(1) + 3(-2) = -7.
  6. \begin{bmatrix} 5 & -6 \\ -1 & 3 \end{bmatrix}. The top left is \displaystyle \frac{1}{2}(4) + 1(3) = 5.
  7. Defined, 3 \times 4: (3 \times 2)(2 \times 4).
  8. Undefined: (2 \times 4)(3 \times 2), and 4 \ne 3.
  9. BC is defined and 2 \times 4. CB is undefined: (4 \times 4)(2 \times 4), and 4 \ne 2.
  10. A^2 is undefined: (3 \times 2)(3 \times 2), and 2 \ne 3. C^2 is defined and 4 \times 4.
  11. PQ = \begin{bmatrix} 6 & 6 \\ -4 & 0 \end{bmatrix} and QP = \begin{bmatrix} 2 & -2 \\ 8 & 4 \end{bmatrix}. They differ.
  12. Both are \begin{bmatrix} 6 & 3 \\ 0 & -3 \end{bmatrix}. R = 3I_2, so RP = 3(I_2 P) = 3P and PR = 3(P I_2) = 3P, by the scalar rule and the identity.
  13. Both are \begin{bmatrix} -4 & 7 \\ -6 & 24 \end{bmatrix}, with AB = \begin{bmatrix} -1 & 2 \\ 0 & 6 \end{bmatrix} and BC = \begin{bmatrix} -3 & 12 \\ 1 & 5 \end{bmatrix} on the way.
  14. A^2 = \begin{bmatrix} 3 & -2 \\ 2 & -1 \end{bmatrix} and A^3 = A^2 A = \begin{bmatrix} 4 & -3 \\ 3 & -2 \end{bmatrix}.
  15. AB = \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}, though neither factor is zero, and BA = \begin{bmatrix} -6 & -12 \\ 3 & 6 \end{bmatrix}.
  16. N\begin{bmatrix} 2.75 \\ 0.80 \end{bmatrix} = \begin{bmatrix} 24.50 \\ 15.00 \end{bmatrix}: a tall vase costs 6(2.75) + 10(0.80) = 24.50 dollars, a short one 15.00 dollars.
  17. 369.00 dollars: \begin{bmatrix} 12 & 5 \end{bmatrix}\begin{bmatrix} 24.50 \\ 15.00 \end{bmatrix} = \begin{bmatrix} 369.00 \end{bmatrix}, from 294 + 75.
  18. HR = \begin{bmatrix} 138 & 184 \\ 129 & 172 \\ 132 & 176 \end{bmatrix}. Row 3, column 2: Reiko earns 4(24) + 4(20) = 176 dollars for such a day on a weekend. RH is not defined: (2 \times 2)(3 \times 2), and 2 \ne 3.
  19. A row of A has 3 entries and a column of B has 4. The rule multiplies first by first, second by second, and so on, so the fourth entry of the column has no partner and the sum cannot be formed.
  20. (A + B)(A + B) is A(A + B) + B(A + B), which is A^2 + AB + BA + B^2, by the distributive law used twice. It equals A^2 + 2AB + B^2 exactly when BA = AB, that is, when A and B commute.
  21. Entry (i, j) of I_m A is \sum_k \delta_{ik} a_{kj}, where \delta_{ik} is 1 for k = i and 0 otherwise. Only the k = i term survives, giving a_{ij}. The sizes match, so I_m A = A.
  22. Exactly the matrices \begin{bmatrix} a & 0 \\ 0 & d \end{bmatrix}. With B = \begin{bmatrix} a & b \\ c & d \end{bmatrix}, DB = \begin{bmatrix} 2a & 2b \\ 3c & 3d \end{bmatrix} and BD = \begin{bmatrix} 2a & 3b \\ 2c & 3d \end{bmatrix}, so 2b = 3b and 3c = 2c, giving b = 0 and c = 0.
kg1128 · updated 2026-10-20 · CC-BY-SA-4.0