Entry (i, j) of AB is row i of A dotted with column j of B. So an m×n times an n×p gives an m×p, and the inner dimensions have to match. Crucially, AB and BA are different matrices — often not even the same shape — so the order is part of the question, not a detail.
What a matrix is for
A matrix is not primarily a grid of numbers. It is a linear transformation written down.
Multiplying a matrix by a vector applies the transformation. Multiplying two matrices composes two transformations into one — and that is where the multiplication rule comes from. It looks arbitrary until you notice it is the only rule that makes composition work.
Reading it that way makes the rest of linear algebra follow. The determinant is how much the transformation scales volume. The inverse is the transformation that undoes it. Eigenvectors are the directions it leaves pointing the same way.
It is also why the identity matrix has ones on the diagonal: that is the transformation that changes nothing.
Why order matters
AB and BA are different. Not occasionally — almost always.
Often they are not even the same shape. A 2×3 times a 3×2 gives a 2×2, and the other way round gives a 3×3. Same two matrices, results of different sizes.
The reason is composition. Rotating and then stretching is not the same as stretching and then rotating, and matrices record that faithfully. Ordinary numbers commute because scaling by 3 then by 5 is the same as 5 then 3; transformations in general are not so obliging.
The consequences reach further than they look. (A + B)² is not A² + 2AB + B², because the cross terms are AB + BA and those cannot be added into 2AB. Cancellation fails too: AB = AC does not give B = C. Almost every algebraic habit that relies on swapping factors is simply false here, which is why this page shows both orders rather than one.
Three different products
“Multiplying two matrices” can mean three different things, and mixing them up is a common source of wrong answers.
Matrix product (A × B). Rows into columns. This is the one that composes transformations, and the only one that most texts call multiplication.
Hadamard product (A ∘ B). Entry by entry, requiring identical shapes. It does not compose anything — but it is exactly what elementwise multiplication means in numerical code and in neural networks, where it is used constantly.
Kronecker product (A ⊗ B). Every entry of A replaced by that entry times the whole of B, so a 2×2 with a 3×3 gives a 6×6. It turns up in quantum computing and in signal processing, where it describes combining two independent systems.
The Hadamard product is commutative. The matrix product is not. That difference alone is a good reason to keep the names apart.
Why the shapes must match
The rules differ between operations, and the reason is not arbitrary.
Addition needs identical shapes, because it works entry by entry and there is nothing to pair a missing entry with. There is no rule that rescues a mismatch.
Multiplication needs the inner dimensions to match: an m×n times an n×p. A row of the first must have exactly as many entries as a column of the second, or the dot product has nothing to pair up.
Reading it as composition makes it obvious. B maps from a p-dimensional space to an n-dimensional one; A then maps from n dimensions onwards. If A expects a different number of dimensions than B produces, the two simply do not connect.
The result being m×p — the two outer dimensions — is the same statement: you start in p dimensions and end in m.
Where it gets used
Graphics. Every rotation, scale and translation is a matrix, and combining them into one transformation is a matrix product. A 3D scene applies several per vertex per frame.
Machine learning. A neural network layer is a matrix multiplication followed by a non-linearity. Training one is mostly very large matrix products, which is precisely what a GPU is built to do.
Systems of equations. Any linear system is Ax = b, and solving it is what most of the rest of linear algebra exists for.
Markov chains. A transition matrix raised to the nth power gives the state after n steps — which is why the power operation on this page uses binary exponentiation rather than multiplying n times.
Sources and methodology
Matrix arithmetic is standard linear algebra; these are the references.
Method. Every entry is an exact rational, so a matrix containing thirds comes back containing thirds rather than a decimal that has already lost information. Powers use binary exponentiation rather than repeated multiplication, which matters once the exponent grows: A¹² is four multiplications rather than eleven. Both AB and BA are computed for a product whenever the shapes allow, and the page reports whether they agree. That engine is verified on every change against 104 hand-written assertions, including that transposing twice returns the original matrix on three hundred generated cases, and that det(AB) equals det(A)·det(B) exactly. The count and the per-case breakdown are published on the formula verification page.
Read the guide
What a square matrix does to area and volume — and when it collapses them — is on the Determinant Calculator.