All courses
Courses › How LLMs really work · foundation

Matrices and shapes

Read a matrix as a table of rows and columns, state its shape, index rows and columns, transpose it.

Builds on: Vectors

▶ Watch this lesson Start the whole course

What the animation says

Twenty numbers, one table

Here's a sentence of five words. Each word is a vector of four numbers. That's twenty numbers floating around. We need to say things like: the third number of word two. Or: every word's third number. So stack the vectors as rows of one table. Row two is word two's vector. That table is a matrix. Its shape is rows by columns, and rows always come first. So what shape is this one?

Rows, columns, one entry

Five by four it is: five rows, one per word, then four columns. A row is one word's whole vector, so it holds four numbers. A column is the same position from every word, so it holds five numbers. To point at one number, give its row, then its column. Row first, counting from one. Tap the number in row two, column three.

A small one, then flip it

Here's a smaller case. Two rows: one, two, three, and four, five, six. Its shape is two by three. Row one, column two holds two. Row two, column one holds four. Row two is four, five, six: length three. Column three is three and six: length two. Now transpose it: flip the table so each row becomes a column. After the flip, what's the shape?

Reading the flipped table

Three by two. Row one, one two three, stands up as column one. Row two becomes column two. It's the same six numbers, just rearranged. To read the flipped table, swap the two indices: its row and column are the original's column and row. Check: the flipped table's row three, column one holds three. The original's row one, column three: also three. Your turn: in the flipped table, tap row two, column one.

Shapes to carry forward

In this course, a list of items, each a vector of length d, stacks into a matrix: one row per item. Its shape is always rows by columns, and an entry is named row first, then column. Transposing swaps rows and columns. An m by n matrix becomes n by m, and the two indices swap. Same numbers, new arrangement. The formulas ahead lean on exactly these shapes.

Many vectors at once: how do we hold them?

A sentence of 5 words, where each word is described by a vector of 4 numbers, gives you 20 numbers. You need to say things like "the 3rd number of word 2" and "all the words' 3rd numbers" without confusion. Formulas later handle thousands of such vectors at once. What object holds them all, and how do you point at one number, one vector, or one slice across all of them?

A matrix is a table of numbers

A **matrix** is a rectangular table of numbers, like a spreadsheet. Stack the word vectors as **rows**: row 2 is word 2's vector. A **column** then holds the same position (say, the 3rd number) from every row. Its **shape** is rows × columns, and rows are always stated first. **Transposing** flips the table so rows become columns: row 1 becomes column 1, row 2 becomes column 2, and so on.

Shape, entries, rows, columns, transpose

$X \in \mathbb{R}^{m\times n}$: $X$ has $m$ rows and $n$ columns of real numbers, so it holds $mn$ numbers in total. Each **entry** is $X_{ij}$: row $i$, column $j$, with the row index first. Indices start at 1. Row $i$ is a vector of length $n$. Column $j$ is a vector of length $m$. The **transpose** is $X^\top \in \mathbb{R}^{n\times m}$, with $(X^\top)_{ij} = X_{ji}$. It holds the same $mn$ numbers, rearranged. In this course, $T$ items with vectors of length $d$ form $X \in \mathbb{R}^{T\times d}$.

Reading and flipping a 2×3 matrix

$$X=\begin{bmatrix}1&2&3\\4&5&6\end{bmatrix}$$ Shape: 2 rows, 3 columns, so $2\times3$. - $X_{12}=2$ (row 1, column 2). $X_{21}=4$. - Row 2 is $(4,5,6)$, which has length 3. - Column 3 is $(3,6)$, which has length 2. $$X^\top=\begin{bmatrix}1&4\\2&5\\3&6\end{bmatrix}$$ The shape is now $3\times2$. Check: $(X^\top)_{31}=3=X_{13}$.

Remember

A matrix is a table. Its shape is rows × columns, and $X_{ij}$ means row $i$, column $j$, with the row always first. Transposing swaps rows and columns, so an $m\times n$ matrix becomes $n\times m$ and $(X^\top)_{ij}=X_{ji}$.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.