Matrices: tables of numbers
Why this matters
A vector holds one list of numbers. A matrix holds many lists at once, as a table of rows and columns. That's the shape almost all data takes before a model sees it: one row per example, one column per measurement. It's also the shape of a model's memory, since a layer's weights are a matrix too, and of a picture, whose pixels sit in rows and columns. This lesson is about reading matrices: their size, how to name one entry, and how to pull out a whole row or column, on paper and in NumPy. Getting row and column the right way around, and remembering that Python counts from 0, are the two habits that prevent most matrix bugs.
- Read a matrix's size as rows × columns, and an entry aᵢⱼ as row i, column j
- See a matrix as a stack of row vectors, a row of column vectors, or a picture
- Index a two-dimensional NumPy array with A[i, j], A[i] and A[:, j], counting from 0
- Store a data set as a matrix X with one row per example and one column per feature, and read the same pieces from a PyTorch tensor
The math: rows, columns and entries
Purpose. Hold many vectors together in one object.
A matrix is a rectangle of numbers. Its size is written rows × columns, always rows first:
In plain words: A has 2 rows and 3 columns, so it's a 2 × 3 matrix holding 6 numbers.
Each number is an entry, named by its row and then its column: a₁₂ is row 1, column 2, which is 7, and a₂₃ is row 2, column 3, which is 3. The rule "row first, then column" never changes, and it's the one to say aloud while you point.