Matrix Multiplication

How to multiply matrices — rows times columns — and why this operation encodes function composition and systems of linear maps.

Matrix multiplication — (AB)ᵢⱼ = row i of A · col j of B
2×3×3×2=2×2— inner dims match ✓
A (2×3)
120314
×
B (3×2)
210312
=
C = AB (2×2)
271014

Hover a result cell to see the row·column dot product

Definition

To multiply matrices AA (size m×nm \times n) and BB (size n×pn \times p), the number of columns of AA must equal the number of rows of BB. The result C=ABC = AB has size m×pm \times p.

The (i,j)(i,j) entry of CC is the dot product of row ii of AA with column jj of BB:

Cij=k=1nAikBkjC_{ij} = \sum_{k=1}^n A_{ik} B_{kj}

Matrix multiplication is:

  • Associative: (AB)C=A(BC)(AB)C = A(BC)
  • Distributive: A(B+C)=AB+ACA(B+C) = AB + AC
  • NOT commutative: ABBAAB \neq BA in general
Key properties
  • Associative: (AB)C=A(BC)(AB)C = A(BC) — a chain of products can be grouped however you like
  • Distributive over addition: A(B+C)=AB+ACA(B+C) = AB+AC and (A+B)C=AC+BC(A+B)C = AC+BC
  • Scalar compatibility: c(AB)=(cA)B=A(cB)c(AB) = (cA)B = A(cB) for any scalar cc
  • Not commutative: ABBAAB \neq BA in general — order matters
  • The identity matrix II acts as a multiplicative identity: AI=IA=AAI = IA = A
Common mistakes
  • Assuming AB=BAAB = BA: matrix multiplication only commutes in special cases (e.g. both diagonal, or one is the identity) — never assume it by default
  • Mismatched dimensions: Am×nBp×qA_{m\times n}B_{p\times q} is only defined when n=pn = p — always check the inner dimensions match
  • (AB)2A2B2(AB)^2 \neq A^2B^2 in general: (AB)(AB)=ABAB(AB)(AB) = ABAB, which simplifies to A2B2A^2B^2 only if AA and BB commute
2×2 multiplication

(1234)(5678)=(15+2716+2835+4736+48)=(19224350)\begin{pmatrix}1&2\\3&4\end{pmatrix}\begin{pmatrix}5&6\\7&8\end{pmatrix} = \begin{pmatrix}1\cdot5+2\cdot7 & 1\cdot6+2\cdot8\\3\cdot5+4\cdot7&3\cdot6+4\cdot8\end{pmatrix} = \begin{pmatrix}19&22\\43&50\end{pmatrix}

Try it

Show that matrix multiplication is not commutative: find 2×22\times2 matrices AA and BB with ABBAAB \neq BA.

Solution

A=(1000)A = \begin{pmatrix}1&0\\0&0\end{pmatrix}, B=(0100)B = \begin{pmatrix}0&1\\0&0\end{pmatrix}.

AB=(0100)AB = \begin{pmatrix}0&1\\0&0\end{pmatrix}, BA=(0000)BA = \begin{pmatrix}0&0\\0&0\end{pmatrix}. So ABBAAB \neq BA.

Related concepts