MatrixStep

Guides6 min

Matrix Multiplication: Rules, Worked Examples and Why AB ≠ BA

Matrix multiplication is not entry-by-entry multiplication. You pair each row of the first matrix with each column of the second, multiply and add, which is easy to get wrong at first. This guide covers everything in order, from the size condition to worked examples.

When can two matrices be multiplied?

If A is an m×n matrix and B is an n×p matrix, the product AB exists and is an m×p matrix. The number of columns of A must equal the number of rows of B. A handy way to remember it: the inner numbers must match, and the outer numbers give the size of the result.

(m × n) · (n × p) = (m × p)

For example, a 2×3 matrix times a 3×2 matrix gives a 2×2 matrix, while a 2×3 matrix times another 2×3 matrix is not defined.

How to compute each entry

The entry in row i, column j of the result is the sum of the products of row i of A and column j of B, taken in order. You can think of it as the dot product of the two vectors.

(AB)ᵢⱼ = aᵢ₁b₁ⱼ + aᵢ₂b₂ⱼ + … + aᵢₙbₙⱼ

Example: a 2×3 matrix times a 3×2 matrix

A =123456B =789101112

A is 2×3 and B is 3×2, so the product exists and is 2×2. Let's compute the entries one at a time.

(AB)₁₁ = 1·7 + 2·9 + 3·11 = 58
(AB)₁₂ = 1·8 + 2·10 + 3·12 = 64
(AB)₂₁ = 4·7 + 5·9 + 6·11 = 139
(AB)₂₂ = 4·8 + 5·10 + 6·12 = 154
AB =5864139154

Changing the order changes the result

With the same two matrices, BA is a 3×3 matrix because B is 3×2 and A is 2×3. The sizes alone are different, so AB and BA cannot even be compared.

BA =3954694968875982105

Even when both matrices are square, so that both products exist, usually AB ≠ BA.

1234×0110=2143but0110×1234=3412

Multiplying on the left and multiplying on the right are different operations. That is why, when you say "multiply A by B", it matters which side B goes on.

Useful properties

Where matrices behave differently from numbers

1000×0001=0000

Common mistakes

FAQ

Can AB exist while BA does not?

Yes. If A is 2×3 and B is 3×3, then AB is a 2×3 matrix, but BA is undefined because B has 3 columns and A has 2 rows.

When multiplying several matrices, does it matter which product I compute first?

No. Multiplication is associative, so any grouping gives the same result. What you cannot do is change the order. Starting with the pair whose sizes fit best often saves intermediate work.

What about multiplying a square matrix by itself several times?

AA is written A². A² is defined only when A is square. Diagonal matrices are easy: just raise each diagonal entry to the power.

Related guides