Matrices: Representation & Basic Operations
Linear Algebra Fundamentals
Chapter 4 · Matrices: Representation & Basic Operations
Chapters 2 and 3 worked entirely with vectors — single ordered lists of numbers. This chapter introduces the other core object of linear algebra: the matrix, a rectangular grid of numbers arranged in rows and columns. A matrix can represent a table of data, an image's pixel grid, or — most importantly for this course — a rule for transforming vectors, which Chapter 5 covers in full.
What a Matrix Is
A matrix with m rows and n columns is called an "m × n matrix." Each individual number is an entry, identified by its row and column position.
| Concept | Vector (Ch.2–3) | Matrix (this chapter) |
|---|---|---|
| Shape | A single row (or column) of numbers | A genuine 2D grid — rows and columns |
| Code equivalent | A flat Python list | A list of lists (or a 2D NumPy array) |
| One way to think about it | A point or direction | A rule for transforming vectors, or a table of data |
Matrix Addition & Scalar Multiplication — The Easy Part
These work exactly like the vector versions from Chapter 2: entry by entry. Addition requires both matrices to have the exact same shape.
Worked example: E = [[2, -1], [0, 4]], F = [[1, 3], [-2, 5]].
| Operation | Result |
|---|---|
| E + F | [[3, 2], [-2, 9]] |
| 3 · E | [[6, -3], [0, 12]] |
Matrix Multiplication — The Real Operation
This is the operation that actually does the interesting work, and it does not mean multiplying entry-by-entry the way addition does. To find the entry at row i, column j of the result, take the dot product (Chapter 2 again) of row i from the first matrix and column j from the second.
A × B to be defined, the number of columns in A must equal the number of rows in B. If A is m × n and B is n × p, the result is m × p — it inherits the "outer" dimensions and the shared "inner" dimension disappears entirely.
Worked example: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]] — both 2×2, so the result is 2×2.
| Entry | Calculation | Result |
|---|---|---|
| Row 0, Col 0 | (1×5) + (2×7) = 5 + 14 | 19 |
| Row 0, Col 1 | (1×6) + (2×8) = 6 + 16 | 22 |
| Row 1, Col 0 | (3×5) + (4×7) = 15 + 28 | 43 |
| Row 1, Col 1 | (3×6) + (4×8) = 18 + 32 | 50 |
So A × B = [[19, 22], [43, 50]].
Why Matrix Multiplication Isn't Commutative
Computing B × A instead — same two matrices, reversed order — gives [[23, 34], [31, 46]], a completely different result from A × B = [[19, 22], [43, 50]].
A × B and B × A happen to be defined, they're usually genuinely different matrices. This isn't a technicality: since a matrix will turn out to represent a transformation in Chapter 5 (a rotation, a scale, a reflection), A × B means "apply B, then apply A" — and applying a rotation then a scale is visibly not the same as scaling first, then rotating.
The dimension rule can make the non-commutativity even starker. With a non-square 2×3 matrix C and a 3×2 matrix D, C × D is a valid 2×2 matrix — but D × C is also valid, and produces a 3×3 matrix. Same two matrices, same multiplication rule, two entirely different-shaped results, purely from the order they're multiplied in.
The Identity Matrix
The identity matrix I is the matrix equivalent of the number 1: multiplying any matrix by it (in either order, where the shapes allow) leaves that matrix completely unchanged. It has 1s down the main diagonal and 0s everywhere else.
Confirmed with the earlier example: A × I = [[1, 2], [3, 4]] — exactly A, unchanged.
Matrices in Code — NumPy
A * B does entry-wise multiplication (multiplying matching positions directly), not the row-by-column matrix multiplication this chapter defines. The @ operator (or np.matmul) is what performs genuine matrix multiplication — mixing the two up is a common source of silently wrong results, since both produce a same-shaped matrix without an obvious error.
Hands-On Exercises
Given G = [[2, 0], [1, 3]] and H = [[4, 1], [2, 5]], compute G × H and H × G by hand, showing each entry's calculation. Confirm the two results are different.
A 2×3 matrix J = [[1, 0, 2], [3, 1, 1]] and a 3×1 matrix (a column vector) K = [[2], [1], [4]] are given. Compute J × K by hand, stating the resulting shape. Then explain, using this chapter's own dimension rule, exactly why K × J is not defined.
A teammate writes NumPy code using A * B where A and B are both 3×3 matrices, intending to perform real matrix multiplication, and is confused that the result "looks wrong" compared to doing the calculation by hand. Explain what's actually happening, what the code should say instead, and why the bug wasn't caught by an error or crash.
Chapter 4 Quick Reference
- A matrix is an
m × ngrid of numbers —mrows,ncolumns - Addition/scalar multiplication: entry-by-entry, same as vectors; addition requires identical shapes
- Matrix multiplication: each output entry is the dot product of a row from the first matrix and a column from the second
- Dimension rule:
A (m×n) × B (n×p) → result (m×p)— the inner dimensions must match and disappear from the result's shape - Not commutative:
A × B ≠ B × Ain general — order matters, and can even change the result's shape entirely - Identity matrix: 1s on the diagonal, 0s elsewhere —
A × I = A, the "do nothing" transformation - In NumPy,
@is real matrix multiplication;*is entry-wise — mixing them up produces a same-shaped but silently wrong result - Next chapter: Matrices as transformations — rotation, scaling, reflection, and composing them