How matrix multiplication works
Multiplying two matrices is not multiplying matching cells. Each cell of the result comes from a whole row of the first matrix and a whole column of the second: pair the row entries with the column entries, multiply each pair, and add the products. That sum is called a dot product, and C[i][j] is the dot product of row i of A with column j of B.
That is why the sizes must line up. A row of A has as many entries as A has columns, and a column of B has as many entries as B has rows; the pairs only match when those two numbers are equal. An n × m matrix times an m × p matrix gives an n × p result. The visualizer bands the current row of A and column of B so you can see each pairing as the sum grows.
The three loops
Two loops pick the result cell, and a third walks along the row and down the column, accumulating the sum:
C = new matrix[n][p]
for i in 0 .. n-1:
for j in 0 .. p-1:
sum = 0
for k in 0 .. m-1:
sum += A[i][k] * B[k][j]
C[i][j] = sum
return CThe index k is the one that moves along both matrices at once: it is the column index in A and the row index in B. Every result cell costs m multiplications, and there are n × p cells, so the whole product costs n × m × p multiplications, the number the counter above reaches.
A worked example: 2 × 3 times 3 × 2
A has 2 rows of 3 and B has 3 rows of 2, so the inner sizes match and the result is 2 × 2. The top-left cell pairs row 1 2 3 with column 1 3 5: 1×1 + 2×3 + 3×5 = 22.
The cell next to it uses the same row with the second column, 2 4 6: 1×2 + 2×4 + 3×6 = 28. The bottom row of A, 4 5 6, gives 49 and 64 the same way. Twelve multiplications in all, three per cell.
Why the order matters
A × B and B × A are different products. With the example above, B × A pairs the 2-entry rows of B with the 2-entry columns of A and gives a 3 × 3 result, not a 2 × 2 one. Even for two square matrices of the same size the two products are normally different, which is unlike the numbers you are used to.
The cost also depends on how you group a chain of products. Multiplying (A × B) × C can be many times cheaper or dearer than A × (B × C), and choosing the best order is the classic dynamic-programming problem called matrix chain multiplication.
Where you'll meet it
- Graphics. Every move, scale, rotation and camera projection in 2D and 3D graphics is a matrix product applied to points.
- Machine learning. A layer of a neural network multiplies its inputs by a weight matrix; training and inference are mostly this loop, run on GPUs.
- Graphs. Multiplying an adjacency matrix by itself counts the two-step paths between every pair of nodes.
- Fast recurrences. The n-th Fibonacci number is a power of a 2 × 2 matrix, computed in O(log n) multiplications.
Mistakes beginners make
- Multiplying cell by cell.
A[i][j] × B[i][j]is a different operation, the element-wise product. It needs equal shapes and is not what matrix multiplication means. - Writing B[j][k]. The inner index
kis the row ofB, so the factor isB[k][j]. - Not resetting the sum.
sum = 0belongs inside thejloop, before thekloop, or each cell inherits the previous total. - Assuming A × B = B × A. It almost never is, and one of them may not exist.
Three nested loops make this the first O(n³) algorithm most people meet. See how that growth compares with the others: Big-O Playground →
Matrix multiplication questions
How do you multiply two matrices step by step?
For each cell of the result, take the matching row of the first matrix and the matching column of the second, multiply them pair by pair and add the products. C[i][j] uses row i of A and column j of B. Repeat for every row and column, which is three nested loops.
When can two matrices be multiplied?
Only when the first matrix has as many columns as the second has rows. An n × m matrix times an m × p matrix gives an n × p matrix. If the inner sizes differ there is no row-times-column pairing, and the product is undefined.
Is matrix multiplication commutative?
No. A × B and B × A are usually different, and one of them may not even exist because of the size rule. Even for two square matrices of the same size the two products normally differ.
What is the time complexity of matrix multiplication?
The three-loop method costs n × m × p multiplications, which is O(n³) for square matrices. Strassen's algorithm brings that down to about O(n^2.81) and research algorithms go lower, but for the sizes met in practice libraries use the three-loop method with clever memory layouts.