Does Matrix Multiplication Order Matter? Explained
The Short Answer
Yes, matrix multiplication order matters. In most cases, AB ≠ BA. This isn't a quirk—it's fundamental to how matrices work. Get used to it.
Matrix multiplication is not commutative. That means the order you multiply matrices matters. Every. Single. Time. Unless specific conditions are met.
Why Order Usually Matters
When you multiply two matrices A and B, you're performing a specific sequence of operations. A×B applies transformation B first, then transformation A. Reverse the order, and you get completely different results.
Think of it like putting on socks, then shoes. Swap the order and you're trying to shove your bare foot into a shoe that's already occupied. Same items, different outcome.
Matrix dimensions enforce this. If A is 3×2 and B is 2×4, you get a 3×4 result. But B×A? That's 2×4 times 3×2—the dimensions don't even match up for most cases. When they do match, the resulting values almost always differ.
When Order Doesn't Matter
There are exceptions. Order doesn't matter when:
- Both matrices are identity matrices of the same size
- Both matrices are zero matrices
- One matrix is a scalar multiple of the identity
- You have diagonal matrices of the same size
- Matrices are specifically constructed to commute (they share eigenvectors)
In practice, these special cases are rare. Most of the time you're working with arbitrary transformation matrices where order is critical.
Where This Shows Up in Practice
Computer Graphics 🎮
In 3D graphics, you multiply transformation matrices constantly. Want to rotate an object, then move it? That's R×T. Want to move it first, then rotate? That's T×R. Completely different results.
Rotate-then-translate: the object spins in place before moving. Translate-then-rotate: the object swings around as if attached to a lever arm. Same operations, different math, visually different outcome.
Machine Learning 🧠
Neural networks chain matrix multiplications through layers. The weight matrices W¹, W², W³ are multiplied in sequence. W³×(W²×(W¹×x)) gives different results than W¹×(W²×(W³×x)).
This is why layer ordering matters in architectures like transformers and MLPs. The math isn't interchangeable.
Physics Equations
Quantum mechanics is full of non-commuting operators. Position and momentum operators, spin operators—order changes outcomes. This isn't a bug. It's the feature that makes quantum mechanics weird.
Linear Systems
When solving Ax = b, you're not multiplying matrices arbitrarily. You're applying operations in a specific sequence to isolate variables. The order is baked into the problem definition.
How to Work With Matrix Multiplication Order
Here's a practical approach:
Step 1: Know What You're Applying
Identify what each matrix represents. Is it a rotation? Translation? Scaling? Projection? Write it down.
Step 2: Determine Application Order
Matrices multiply right-to-left in standard notation. If you want to Apply A, then B, then C to vector v, you compute C×(B×(A×v)) or equivalently CBA×v.
Step 3: Verify Dimensions
Check that inner dimensions match. (m×n) × (n×p) works. (n×m) × (n×p) doesn't.
Step 4: Test With Simple Cases
Use identity matrices and simple vectors to verify your order. Identity matrices don't change values, so they let you isolate the effect of other matrices.
Quick Reference: Common Transformation Orders
| Operation Sequence | Matrix Expression | Result |
|---|---|---|
| Rotate, then Translate | T × R | Object spins in place, then moves |
| Translate, then Rotate | R × T | Object swings around pivot point |
| Scale, then Rotate | R × S | Object stretches, then spins |
| Rotate, then Scale | S × R | Object spins, then stretches along axes |
The Bottom Line
Matrix multiplication order matters. This isn't negotiable. Get comfortable reading expressions right-to-left, and always verify your order before committing to code.
If your code produces unexpected results, check the multiplication order first. It's usually the culprit.