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.

# A 2x2 matrix, represented as a list of rows A = [ [1, 2], [3, 4], ] # A[0] is the first row: [1, 2] # A[1][0] is the entry in row 1, column 0: 3
ConceptVector (Ch.2–3)Matrix (this chapter)
ShapeA single row (or column) of numbersA genuine 2D grid — rows and columns
Code equivalentA flat Python listA list of lists (or a 2D NumPy array)
One way to think about itA point or directionA 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]].

OperationResult
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.

The dimension rule
For 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.
def matmul(A, B): rows_A, cols_A = len(A), len(A[0]) rows_B, cols_B = len(B), len(B[0]) assert cols_A == rows_B, "inner dimensions must match" result = [[0]*cols_B for _ in range(rows_A)] for i in range(rows_A): for j in range(cols_B): result[i][j] = sum(A[i][k] * B[k][j] for k in range(cols_A)) return result

Worked example: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]] — both 2×2, so the result is 2×2.

EntryCalculationResult
Row 0, Col 0(1×5) + (2×7) = 5 + 1419
Row 0, Col 1(1×6) + (2×8) = 6 + 1622
Row 1, Col 0(3×5) + (4×7) = 15 + 2843
Row 1, Col 1(3×6) + (4×8) = 18 + 3250

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 ≠ B × A, in general
Unlike ordinary number multiplication, order matters for matrices — even when both 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.

I = [ [1, 0], [0, 1], ] # A x I == A, always

Confirmed with the earlier example: A × I = [[1, 2], [3, 4]] — exactly A, unchanged.

Forward reference — where this is going
Chapter 5 uses exactly this machinery to represent transformations: a rotation matrix, a scale matrix, a reflection matrix — each one is just a specific grid of numbers that, when multiplied against a vector, produces the transformed version of that vector. The identity matrix is, unsurprisingly, "the transformation that does nothing."

Matrices in Code — NumPy

import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) print(A + B) # entry-wise addition print(A @ B) # real matrix multiplication — NOT A * B print(np.eye(2)) # the 2x2 identity matrix
A real NumPy gotcha
In 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

Exercise 1

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.

📄 View solution
Exercise 2

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.

📄 View solution
Exercise 3

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.

📄 View solution

Chapter 4 Quick Reference

  • A matrix is an m × n grid of numbers — m rows, n columns
  • 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 × A in 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