Alex Loftus
← Back to the lesson

Matrix multiplication is a grid of dot products

The inner dimensions match because every row-column dot product needs the same number of entries.
The inner dimensions match because every row-column dot product needs the same number of entries.

One output entry is one dot product

Think of a row as a rule and a column as an input. To apply that rule, multiply matching entries and add. A matrix product performs all the compatible row–column pairings at once.

For example,

[1234][56]=[1×5+2×63×5+4×6]=[1739].\begin{bmatrix}1&2\\3&4\end{bmatrix} \begin{bmatrix}5\\6\end{bmatrix} =\begin{bmatrix}1\times5+2\times6\\3\times5+4\times6\end{bmatrix} =\begin{bmatrix}17\\39\end{bmatrix}.

The first row produces the first output. The second row produces the second. Both read the same two input coordinates.

The shapes tell a story

If AA has shape m×nm\times n and BB has shape n×pn\times p, then

A⏟m×nB⏟n×p=AB⏟m×p.\underbrace{A}_{m\times n} \underbrace{B}_{n\times p} =\underbrace{AB}_{m\times p}.

The shared nn is the number of terms in each dot product. The output has one row for each rule in AA and one column for each input in BB.

In symbols,

(AB)ij=∑k=1nAikBkj.(AB)_{ij}=\sum_{k=1}^{n}A_{ik}B_{kj}.

The index kk travels along a row of AA and down a column of BB. The indices i,ji,j tell us where to put the answer.

Try a second input

What happens if we add the input column (0,1)(0,1) beside (5,6)(5,6)?

Answer: We get a second output column, (2,4)(2,4). The same matrix applies its rules to each input independently.