DSA Visualizer
MATRIX

Multiply Two Matrices

C = A × B, one multiply-add at a time. The current row of A and column of B are banded while the sum builds in the result.

C = A × B: every cell of C is a row of A dotted with a column of B, one multiply-add at a time.

0
1
2
0
1
2
3
1
4
5
6
A · 2 × 3
0
1
0
1
2
1
3
4
2
5
6
B · 3 × 2
0
1
0
·
·
1
·
·
A × B · 2 × 2
A is 2 × 3 and B is 3 × 2, so C = A × B is 2 × 2. Each C[i][j] is row i of A dotted with column j of B.
0
MultsMultiplications
0
WritesWrites
4
LeftRemaining
1 / 18

Pseudocode

Multiply
1C = new matrix[n][p]
2for i in 0 .. n-1:
3 for j in 0 .. p-1:
4 sum = 0
5 for k in 0 .. m-1:
6 sum += A[i][k] * B[k][j]
7 C[i][j] = sum
8return C

Legend

Current rowCurrent columnReadingWritingWritten

Matrix size

Want your own values? Edit this grid in the 2D Array Visualizer →

Tip: Space play/pause · ← → step

Learn multiply

Multiply builds a new matrix C where C[i][j] is the dot product of row i of A and column j of B: multiply the pairs, add them up. It only works when A has as many columns as B has rows, and the result is rows-of-A × cols-of-B.

Unlike the other three, multiplication makes new numbers instead of moving old ones around, and every cell of the result costs a whole loop of its own. Compare with transpose, rotate 90° and flip.

Three nested loops, so it runs in O(n · m · p) time, which is O(n³) for square matrices. The result needs its own n × p cells.

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 C

The 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 k is the row of B, so the factor is B[k][j].
  • Not resetting the sum. sum = 0 belongs inside the j loop, before the k loop, 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.

Open in DSA Visualizer ↗