How Matrix Multiplication Works, With Worked Examples
Learn the row-by-column rule for multiplying matrices, why the dimensions must match, why order matters, and what the product means geometrically.
Matrix multiplication looks strange the first time you meet it. Instead of multiplying matching entries, you combine rows of the first matrix with columns of the second, and each output entry is a sum of products. The rule seems arbitrary until you see that it is exactly what you need for composing linear transformations, solving systems, and running many computations at once.
This guide gives the rule, works a concrete example, explains the dimension requirement, and shows why the order of the factors matters.
The row-by-column rule
Suppose you multiply matrix A by matrix B to get matrix C. The entry in row i and column j of C is found by taking row i of A and column j of B. Multiply the first entry of the row by the first entry of the column, the second by the second, and so on. Then add all those products. That sum is the entry C at row i, column j.
Each entry of the result is a dot product of one row and one column. There are no cross-row or cross-column interactions beyond that.
A worked example
Let A be the matrix with rows (1, 2) and (3, 4). Let B be the matrix with rows (0, 1) and (1, 0).
The top-left entry of AB takes row 1 of A, which is (1, 2), and column 1 of B, which is (0, 1). The product is 1 times 0 plus 2 times 1, which equals 2.
The top-right entry takes row 1 of A and column 2 of B, which is (1, 0). That gives 1 times 1 plus 2 times 0, which equals 1.
The bottom-left entry takes row 2 of A, which is (3, 4), and column 1 of B. That gives 3 times 0 plus 4 times 1, which equals 4.
The bottom-right entry gives 3 times 1 plus 4 times 0, which equals 3.
So AB has rows (2, 1) and (4, 3). Each step is a short sum, and the full answer is four such sums.
The dimension rule
Matrix multiplication only works when the number of columns in the first matrix equals the number of rows in the second. If the first matrix is m by n, meaning m rows and n columns, the second must be n by p. The product is then m by p. The inner dimensions cancel and the outer dimensions remain.
The table shows several cases.
| First matrix | Second matrix | Product | Valid? |
|---|---|---|---|
| 2 by 3 | 3 by 4 | 2 by 4 | Yes |
| 3 by 3 | 3 by 1 | 3 by 1 | Yes |
| 2 by 2 | 2 by 2 | 2 by 2 | Yes |
| 3 by 3 | 2 by 3 | none | No |
| 1 by 4 | 1 by 4 | none | No |
When the inner numbers do not match, the product is undefined. Checking dimensions first saves time and catches many mistakes.
Order matters
Matrix multiplication is generally not commutative. In general, AB is not equal to BA, and you should never assume that you can swap the order.
Using the matrices above, BA is computed the same way. The top-left entry is 0 times 1 plus 1 times 3, which equals 3. The top-right is 0 times 2 plus 1 times 4, which equals 4. The bottom-left is 1 times 1 plus 0 times 3, which equals 1. The bottom-right is 1 times 2 plus 0 times 4, which equals 2. So BA has rows (3, 4) and (1, 2). This differs from AB, which had rows (2, 1) and (4, 3).
Some matrices do commute, such as a matrix with itself, or two diagonal matrices. Those cases are exceptions, and you should verify them rather than assume them.
Why the rule is shaped this way
The row-by-column rule is designed so that multiplication composes transformations. A matrix represents a linear map, a function that takes a vector and returns a new vector. Multiplying two matrices gives the matrix of the map that applies the second transformation first and then the first one.
This means AB applies B, then A. Reading right to left matches the function order, just as f of g of x means apply g first. That is why AB and BA can differ: applying a rotation then a stretch is not the same as applying the stretch then the rotation.
The rule also keeps the useful properties. Matrix multiplication is associative, so A times B times C equals A times the product of B and C, and also equals the product of A and B times C. It is distributive over addition, so A times the sum of B and C equals AB plus AC.
The cost of multiplying
Multiplying an m by n matrix by an n by p matrix uses m times n times p multiplications, plus about the same number of additions. A 100 by 100 product therefore takes about one million multiplications. Doubling each dimension multiplies the work by eight.
This cost is why the order of a chain of products can matter for speed. When multiplying three matrices, the grouping can change the total work greatly, and optimized libraries choose the grouping for you.
Where matrix multiplication appears
Computer graphics stacks transformations such as rotation, scaling, and translation by multiplying their matrices, so one product applies all of them to every point of a model. Each layer of a neural network applies a matrix multiplication to its inputs and then adds a bias. Markov chains use repeated multiplication by a transition matrix to forecast the probability of states after several steps.
In each case, the work reduces to the same row-by-column rule. Understanding that rule makes these applications much easier to read.
Common mistakes
The most frequent error is multiplying matching entries directly, as if you were adding or using the Hadamard product. Matrix multiplication always uses rows against columns.
Another error is ignoring the dimension check and trying to multiply incompatible shapes. Write the dimensions beside each matrix before you begin.
A third mistake is assuming the identity matrix is the only special case. The identity matrix leaves any compatible matrix unchanged, which is useful, but many other matrices also have special behavior, such as zero matrices and inverses. Check each one.
Key takeaways
To multiply matrices, take each row of the first matrix and each column of the second, multiply them entry by entry, and add the products. The result has as many rows as the first matrix and as many columns as the second.
The product is defined only when the inner dimensions match, and the order of multiplication usually changes the answer. The rule exists because it composes linear transformations, so the product always means one transformation after another.