Matrix Multiplication Compatibility: The Inner-Dimensions Rule
You cannot multiply just any two matrices. Multiplication is defined only when their sizes line up in a specific way, and this compatibility check is always the very first step of any product.
The Idea: A Row Meets a Column
When you multiply A by B, you take each row of A and pair it against each column of B, multiply corresponding entries, and add. For that pairing to work, a row of A must have exactly as many entries as a column of B.
Think of a handshake: each finger of one hand must meet a finger of the other. If one hand has 4 fingers and the other has 3, the handshake fails.
The Precise Statement
Let A be m×n and B be p×q.
A×B is defined if and only if n=p — the number of columns of A equals the number of rows of B. The product C=AB then has order m×q.
Writing the sizes side by side, (m×n)(p×q), the inner numbers (n,p) must match; the outer numbers (m,q) give the result's shape.
Why the Rule Exists
Each entry of the product is
cij=∑k=1naikbkj.
Here k runs over the columns of A (up to n) and the rows of B (up to p). If n=p, the sum runs out of matching terms and is meaningless — that is exactly why compatibility demands n=p.
Even when both AB and BA are defined, they usually differ. For A of order 2×3 and B of order 3×2, AB is 2×2 but BA is 3×3 — different sizes entirely. Matrix multiplication is not commutative.
Quick Check
| A | B | Defined? | Result | …