If A is an m×n matrix and B an n×p matrix, the product AB is an m×p matrix. The entry in row i and column j
of AB is row i of A times column j of B. When A has three columns and B three rows, that is
- cᵢⱼ = aᵢ₁b₁ⱼ + aᵢ₂b₂ⱼ + aᵢ₃b₃ⱼ
With n columns in A the sum has n terms, aᵢ₁b₁ⱼ + … + aᵢₙbₙⱼ. It is the
dot product of a row and a column. The card above computes
one entry; for a 2×2 product, set the two numbers of the third term to 0.
Holds when A has as many columns as B has rows. Otherwise AB is not defined.
Worked example
A has the rows (1, 2, 3) and (4, 5, 6), and B has the rows (7, 8), (9, 10) and (11, 12). A is 2×3 and B is
3×2, so AB is 2×2.
The entry in row 2, column 1 is row 2 of A, (4, 5, 6), times column 1 of B, (7, 9, 11):
4 · 7 + 5 · 9 + 6 · 11 = 28 + 45 + 66 = 139.
All four entries the same way:
|
column 1 |
column 2 |
| row 1 |
1 · 7 + 2 · 9 + 3 · 11 = 58 |
1 · 8 + 2 · 10 + 3 · 12 = 64 |
| row 2 |
4 · 7 + 5 · 9 + 6 · 11 = 139 |
4 · 8 + 5 · 10 + 6 · 12 = 154 |
The order matters
In the same example BA is a 3×3 matrix, with the rows (39, 54, 69), (49, 68, 87) and (59, 82, 105). AB and
BA are not even the same size. For two square matrices they are usually different as well. With A having
the rows (1, 2) and (3, 4) and B the rows (0, 1) and (1, 0):
| Product |
Rows |
What B does |
| AB |
(2, 1) and (4, 3) |
swaps the columns of A |
| BA |
(3, 4) and (1, 2) |
swaps the rows of A |
| Entry by entry (not the matrix product) |
(0, 2) and (3, 0) |
— |
The last line is what multiplying the entries in the same position gives. It is a matrix of the right size,
and it is not AB.
A second example: a row and a column that give 0
Row (2, −1, 3) times column (4, 5, −2) gives 8 − 5 − 6 = −3. Row (1, −2, 2) times column (4, 3, 1) gives
4 − 6 + 2 = 0. So an entry can be 0 although no number in the row or the column is 0: the row and the
column are then orthogonal as vectors.
Where to go next
A matrix times its inverse is the identity matrix, which is the check on the page on the
inverse of a 2×2 matrix. A linear system can be
written as a matrix product, Ax = b, and is solved by
Gaussian elimination; how many free variables its
solution has is given by the rank.
Common mistakes
- Entry by entry. The product takes row times column, not the entries in the same position.
- Row times row. The second factor is read by columns: b₁ⱼ, b₂ⱼ, b₃ⱼ down column j.
- AB = BA. The order cannot be swapped in general; AB can exist when BA does not.
- The wrong size. The number of columns of A must equal the number of rows of B, and the product has the
rows of A and the columns of B.
Frequently asked questions
What size is the product?
An m×n matrix times an n×p matrix gives an m×p matrix: the rows of the first and the columns of the
second. The two inner numbers, n and n, have to agree.
When is AB = BA?
Only in particular cases, for example when one of them is the identity matrix, when both are diagonal, or
when B is a power of A. As a rule AB ≠ BA.
How do I multiply a matrix by a vector?
A column vector with n entries is an n×1 matrix, so Ax is computed the same way: each entry of the result
is a row of A times x.