Matrix Multiplication
What matrix multiplication means
A matrix is just a grid of numbers arranged in rows and columns — in C we store it as a 2D array. Multiplying two matrices combines them into a new matrix, but not by simply multiplying matching cells. Instead, each cell of the result is built from a whole row of the first matrix and a whole column of the second.
That "row times column" idea is the heart of the topic. Once it clicks, the C program is just three loops that automate it.
The rule: when can you multiply?
You cannot multiply any two matrices. There is one strict rule:
The columns of the first matrix must equal the rows of the second. If A is m×n and B is n×p, the result C is m×p. The two inner n values must match.
For our example both matrices are 2×2, so columns of A (2) equal rows of B (2), and the result is also 2×2.
How one result cell is calculated
Let's take two 2×2 matrices and compute just the top-left cell, so you see the pattern:
A = | 1 2 | B = | 5 6 |
| 3 4 | | 7 8 |
c[0][0] = (row 0 of A) . (col 0 of B)
= 1*5 + 2*7
= 5 + 14
= 19So the top-left result is 19. We repeat the same row-times-column idea for every cell: c[0][1] = 1*6 + 2*8 = 22, and so on.
The full program
Here is the complete C program for 2×2 matrices, with the result matrix started at zero.
#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int c[2][2] = {0}; // start every cell at 0
for (int i = 0; i < 2; i++) // each row of result
for (int j = 0; j < 2; j++) // each column of result
for (int k = 0; k < 2; k++) // shared dimension
c[i][j] += a[i][k] * b[k][j];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}19 22
43 50
Why three nested loops?
Each loop has a clear job:
| Loop | Variable | Its job |
|---|---|---|
| Outer | i | Chooses the result row |
| Middle | j | Chooses the result column |
| Inner | k | Adds up the row×column products |
The outer two loops point at one result cell; the inner loop fills that cell's value by walking along the shared dimension. That is exactly the row-times-column rule turned into code.
Common mistakes
- Forgetting to initialise the result matrix to 0, so
+=adds onto garbage. - Multiplying cells directly (
a[i][j] * b[i][j]) instead of doing row × column. - Ignoring the dimension rule and trying to multiply incompatible matrices.
- Swapping the index order in
a[i][k] * b[k][j]— the sharedkmust line up.
Extend the program to multiply a 2×3 matrix by a 3×2 matrix. Change the array sizes and loop limits, and confirm the result is 2×2. This proves you understand the dimension rule.
Summary
- Matrices multiply by combining a row of the first with a column of the second.
- Columns of the first must equal rows of the second (m×n times n×p gives m×p).
- Each cell
c[i][j]sumsa[i][k]*b[k][j]overk. - Three nested loops: row, column, and the shared dimension.
- Always initialise the result matrix to 0 before adding.
Frequently Asked Questions
When can two matrices be multiplied in C?
How is each element of the result matrix calculated?
c[i][j] is the sum of products of the i-th row of the first matrix with the j-th column of the second. You multiply corresponding elements and add them: c[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j] + ....Why does matrix multiplication in C need three nested loops?
i and column j). The third, innermost loop k walks along the shared dimension to compute the row-times-column sum for that cell. So you need all three: two to locate the cell and one to build its value.Why must the result matrix start at zero?
+=. If the cell starts with garbage instead of 0, the final sum will be wrong. Initialising the result matrix to all zeros gives each cell a clean starting point.