Linear Algebra · Theory · M2
M2-T2 — Matrix Operations and Multiplication
Matrix multiplication looks like arbitrary arithmetic until you see what it is for. Its rules, and above all its order, explain many bugs.
Chaining a sensor calibration, a rotation and a unit conversion is a product of matrices. If you can multiply them, check their shapes and predict what happens when you swap them, you can read most of the linear algebra in any robotics code base.
Learning objectives
After this lesson, you should be able to:
- Add, subtract and scale matrices, and know when two matrices can be added.
- Multiply two matrices with the row-times-column rule and state the shape rule (m × n)(n × p) = (m × p).
- Explain why AB and BA are generally different, and which laws (associativity, distributivity) do hold.
- Derive (AB)ᵀ = BᵀAᵀ and use it to simplify expressions.
Introduction and motivation
A robot gets a raw measurement in a sensor frame, converts units, rotates into the robot frame, and then transforms into the world frame. Each step is a matrix; the whole pipeline is their product. Getting the order wrong is a classic bug: the code runs, the numbers look plausible, and the robot is a few metres off.
This lesson sets up the rules so that you can write such pipelines correctly. The geometric meaning of the product, composition of transformations, comes in its own lesson; here we focus on the arithmetic and the rules it obeys.
The intuition
Adding matrices is simple: matrices of the same shape are added entry by entry, and multiplying by a number scales every entry. These behave exactly like vectors.
The product is different. To get the entry in row i and column j of AB, take row i of A and column j of B, multiply them entry by entry, and add. That is a dot product of two lists of the same length, which is why the number of columns of A must equal the number of rows of B.
Interactive figure. An F-shaped outline is transformed by a rotation and a stretch in two different orders. The green shape is stretch first then rotate and the orange shape is rotate first then stretch. The readout compares the two product matrices entry by entry. This figure needs JavaScript to be interactive.
Why such a strange rule? Because it is the rule that makes (AB)x = A(Bx): multiplying by AB does the same thing as multiplying by B first and then by A. Everything else follows from that purpose, as the next lessons show.
Geometric and physical interpretation
Geometric view. If B moves the plane and then A moves the result, the single matrix AB does both at once, with B acting first. The right-hand factor acts first, which is why products read “backwards” compared with the order of events. Physical view. A rotation followed by a scaling is not the same as the scaling followed by the rotation (a stretched object rotated is not a rotated object stretched). That physical fact is the reason matrix multiplication cannot be commutative.
The mathematics
- A, B
- matrices; A is m × n and B is n × p in a product AB.
- (AB)ij
- the entry of the product in row i, column j.
- I
- the identity matrix, with AI = IA = A.
- c
- an ordinary number (a scalar).
Addition and scaling
(A + B)ij = aij + bij (cA)ij = c · aij
What it means. Matrices of identical shape add entry by entry, and a scalar multiplies every entry. There is nothing hidden here: these are the vector rules applied to a grid.
The product
(AB)ij = ai1·b1j + ai2·b2j + ... + ain·bnj = Σk aik·bkj
What it means. The (i, j) entry is the dot product of row i of A with column j of B. It is defined only when the number of columns of A equals the number of rows of B.
A: m × n, B: n × p ⇒ AB: m × p
What it means. The inner sizes must match and then disappear; the outer sizes remain. Checking this rule in your head catches most multiplication bugs before the code runs.
Laws that hold and laws that fail
- Associative:
(AB)C = A(BC). You may group a chain of products any way you like, which lets you precompute a combined calibration. - Distributive:
A(B + C) = AB + AC. - Identity:
AI = IA = A. - Not commutative:
AB ≠ BAin general. Even when both products exist they usually differ, and one may exist while the other does not. - Transpose reverses order:
(AB)ᵀ = BᵀAᵀ, derived below.
The right-hand factor acts first. AB means “do B, then do A”. Swapping the factors changes the result.
Step-by-step derivation: (AB)ᵀ = BᵀAᵀ
Let A be m × n and B be n × p, so AB is m × p and (AB)ᵀ is p × m. The product BᵀAᵀ is (p × n)(n × m) = p × m, so the shapes agree. Now compare entries.
- By the definition of the transpose, the (i, j) entry of
(AB)ᵀis the (j, i) entry ofAB. - By the product rule,
(AB)ji = Σk ajk bki. - The entries of the transposes are
(Bᵀ)ik = bkiand(Aᵀ)kj = ajk. - Substitute:
Σk ajk bki = Σk (Bᵀ)ik (Aᵀ)kj, which is exactly the (i, j) entry ofBᵀAᵀby the product rule. - Every entry agrees, so
(AB)ᵀ = BᵀAᵀ.
The order reverses for the same reason that taking off socks and shoes reverses the order of putting them on. The same reasoning gives (ABC)ᵀ = CᵀBᵀAᵀ and, later, (AB)⁻¹ = B⁻¹A⁻¹.
Why associativity holds. Both (AB)C and A(BC) have (i, j) entry ΣkΣl aik bkl clj: the order of the two summations does not matter, so the two groupings agree.
A worked example: the order of a product matters
Let A = [[1, 2], [3, 4]] and B = [[0, 1], [1, 0]] (B swaps the two coordinates of a vector).
- AB. Row 1 of A is (1, 2): with column 1 of B, (0, 1) gives 1·0 + 2·1 = 2; with column 2, (1, 0) gives 1·1 + 2·0 = 1. Row 2 is (3, 4): 3·0 + 4·1 = 4 and 3·1 + 4·0 = 3. So
AB = [[2, 1], [4, 3]]: the columns of A were swapped. - BA. Row 1 of B is (0, 1): with column 1 of A, (1, 3) gives 3; with column 2, (2, 4) gives 4. Row 2 is (1, 0): 1 and 2. So
BA = [[3, 4], [1, 2]]: the rows of A were swapped. - Compare.
AB ≠ BA. Multiplying by B on the right acts on columns; on the left it acts on rows. - Transpose rule.
(AB)ᵀ = [[2, 4], [1, 3]]andBᵀAᵀ = [[0, 1], [1, 0]] [[1, 3], [2, 4]] = [[2, 4], [1, 3]]. They agree, whereasAᵀBᵀ = [[3, 1], [4, 2]]does not. - Shapes. A 3 × 2 matrix times a 2 × 4 matrix is 3 × 4; the reverse product (2 × 4)(3 × 2) is not even defined.
Robotics application
Chaining calibrations. A raw measurement x is scaled by a unit-conversion matrix S and then rotated by R into the robot frame. As a pipeline, y = R(Sx). By associativity y = (RS)x, so the combined matrix RS can be computed once at start-up and applied to a million points. Writing SR instead would be wrong whenever the scaling is not the same in all directions.
Point clouds as rows. If a cloud is stored with one point per row, P is N × 3 and transforming every point by a rotation R is P Rᵀ, not R P. The transpose rule explains why: (R Pᵀ)ᵀ = P Rᵀ. This one identity resolves the most common point-cloud shape bug; see how the SLAM track applies transforms to clouds.
Uncertainty. When Module 9 transforms an uncertainty ellipse by a matrix A, the new covariance is A Σ Aᵀ, a product of exactly this form, and its symmetry is guaranteed by (AΣAᵀ)ᵀ = AΣᵀAᵀ.
Batch computation. Estimators accumulate sums like AᵀA over thousands of measurements; these are matrix products whose shape rule (n × m)(m × n) = n × n you will meet in the least-squares module.
Engineering implications and limitations
Practical rules
- Check shapes before multiplying. Write the shape rule in a comment next to every product, and assert shapes in code.
- Know your library's operator. In NumPy
A * Bis entry-by-entry multiplication, andA @ Bis the matrix product. Confusing them runs without error. - Order of grouping changes cost, not value. For a long chain with a vector on the end,
A(B(Cx))costs far less than(ABC)xwhen applied once, whereas precomputingABCwins when applied to many vectors. - Floating-point products are not exactly associative. Mathematically
(AB)C = A(BC); numerically the results can differ in the last digits, which matters when comparing for equality. - Do not square the condition number casually. Forming
AᵀAis convenient but squares the sensitivity of a problem. Module 5 shows what that costs.
Limits
The shape rule only governs whether a product exists. It does not tell you whether the product is meaningful: multiplying a rotation of one frame by a matrix expressed in another frame is shape-correct and physically wrong. Track the frame of each matrix as carefully as its shape.
Common misconceptions
“AB = BA, like multiplying numbers.”
Almost never. With the swap matrix B above, AB swaps the columns of A while BA swaps its rows. Commuting is a special coincidence, for instance for two rotations in the plane or two diagonal matrices.
“Matrix multiplication multiplies matching entries.”
That is the entrywise (Hadamard) product. The matrix product combines each row of the left matrix with each column of the right matrix through dot products.
“(AB)ᵀ = AᵀBᵀ.”
The order reverses: (AB)ᵀ = BᵀAᵀ. The wrong version often even has the right shape, which makes the bug silent.
“If AB = 0 then A = 0 or B = 0.”
Not for matrices. Two non-zero matrices can multiply to zero. This matters later when a non-zero matrix sends non-zero inputs to zero (the null space).
Engineering takeaways
- Matrices of the same shape add entry by entry; scalars scale every entry.
- The product (AB)ij is row i of A dotted with column j of B, and the shape rule (m × n)(n × p) = (m × p) must hold.
- AB means “do B first, then A”; the order matters and AB ≠ BA in general.
- Matrix multiplication is associative and distributive, and the transpose of a product reverses the order: (AB)ᵀ = BᵀAᵀ.
- A calibration pipeline is a product of matrices; combine it once, in the right order, and reuse it.
References and further reading
Primary papers and authoritative documentation.
- MIT 18.06SC Linear Algebra (OCW): lectures on matrix multiplication and its interpretations
- Immersive Linear Algebra: chapter 6, The Matrix
- Boyd and Vandenberghe, Introduction to Applied Linear Algebra (VMLS): chapter on matrix multiplication; the site did not load when we last checked
- Modern Robotics (Lynch and Park), free preprint: appendix of useful formulas
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A is 4 × 3, B is 3 × 5 and C is 5 × 2. Which of the products AB, BA, ABC, CBA exist, and what are the shapes of those that do?
Explanation
AB exists (inner sizes 3 and 3) and is 4 × 5. BA does not exist (inner sizes 5 and 4). ABC exists: (AB) is 4 × 5 and C is 5 × 2, so it is 4 × 2. CBA does not exist (C is 5 × 2 and B is 3 × 5: inner sizes 2 and 3). The shape rule decides everything before any numbers are computed.
Question 2
A robot scales sensor readings with S = diag(2, 1) and rotates them with R (a 90° turn). A colleague computes SR where you computed RS. For the vector (1, 0) what does each give, and why is the difference not a rounding issue?
Explanation
R S (1, 0): S sends (1, 0) to (2, 0), then R turns it to (0, 2). S R (1, 0): R turns (1, 0) to (0, 1), then S leaves it at (0, 1) because only x is stretched. The results (0, 2) and (0, 1) differ by a whole unit: scaling and rotating do not commute when the scaling is not uniform, so the order is part of the definition of the calibration.
Question 3
A point cloud is stored as an N × 3 matrix P. Write the matrix expression that applies a rotation R (3 × 3) to every point, and justify it with the transpose rule.
Explanation
Each point is a row p, and the rotated point as a column vector is R pᵀ. As a row it is (R pᵀ)ᵀ = p Rᵀ. Stacking all N rows gives P Rᵀ, an N × 3 matrix. Writing R P would not even be shape-valid for N ≠ 3, and for N = 3 it would silently rotate the wrong thing.
What’s next
With the product defined, the most important special case is a matrix times a single vector. That is how every robot measurement is transformed, and it has two equally useful readings.
Related content
Nothing below is required to finish this lesson.
- BeforeM2-T1: Introduction to Matrices
Read a matrix as a table of robot data and as a machine that turns vectors into vectors, and use its shape, entries and transpose correctly.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.