Linear algebra for AI: vectors and matrices

Computers can't read words or photos directly: everything is turned into numbers arranged in lists and tables. Linear algebra is the maths of those lists (vectors) and tables (matrices). Almost every AI calculation, from a product recommendation to ChatGPT's next word, is mostly matrices being multiplied very fast on GPUs.

Everyday analogy

An Excel sheet. One cell is a single number (a scalar), one row is a vector, and the whole sheet is a matrix. An AI model is a giant spreadsheet calculation, repeated billions of times.

Example

One member's record as a vector: [age 42, years insured 6, claims last year 3, premium 4,800]. Stack 10,000 members together and you have a 10,000 × 4 matrix: 10,000 rows, 4 columns.

The four building blocks

NameWhat it isExampleIn AI
ScalarA single number5A learning rate, a bias
VectorA list of numbers (1 dimension)[42, 6, 3]One customer; one word's embedding
MatrixA table of rows × columns (2 dimensions)3 rows × 4 columnsA data set; the weights of one layer
TensorNumbers in 3 or more dimensionsA colour photo: height × width × 3 coloursImages, video, batches of data

Operations you'll meet

OperationWhat it doesExample
AdditionAdds matching positions; sizes must match[1, 2] + [3, 4] = [4, 6]
Scalar multiplicationMultiplies every entry by one number2 × [1, 3] = [2, 6]
Dot productMultiplies matching positions and adds them up, giving one number[1, 2, 3] · [4, 5, 6] = 4 + 10 + 18 = 32
Matrix multiplicationRows of the first × columns of the second; inner sizes must match(2 × 3) times (3 × 2) gives a 2 × 2 result
TransposeSwaps rows and columnsA 2 × 3 matrix becomes 3 × 2
Identity matrix1s on the diagonal, 0s elsewhere; multiplying by it changes nothingWorks like the number 1
InverseThe "undo" matrix: A × A⁻¹ gives the identityUsed to solve systems of equations
Length (norm)How long a vector is[3, 4] has length √(9 + 16) = 5

Where AI uses it

In AIThe linear algebra behind it
One neuronA dot product: inputs · weights + bias
A whole layerOne matrix multiplication handles every neuron at once
Search by meaning (RAG, recommendations)Cosine similarity: the dot product divided by the two lengths, measuring how closely two vectors point the same way
Linear regressionSolving a system of equations, written as y = Xβ
Shrinking data (PCA)Eigenvectors find the directions where the data spreads most; keep the top few
Worked example: cosine similarity
Spending on [groceries, travel, health]
Customer A = [2, 0, 1]
Customer B = [4, 0, 2]   (same habits, twice the size)
Customer C = [0, 3, 0]   (only travels)

A · B = 2×4 + 0×0 + 1×2 = 10
|A| = √5 ≈ 2.24   |B| = √20 ≈ 4.47
cos(A, B) = 10 ÷ (2.24 × 4.47) = 1.0  → identical pattern

A · C = 0  → cos(A, C) = 0  → nothing in common

Eigenvectors and PCA, in plain words

An eigenvector of a matrix is a direction that the matrix only stretches or shrinks, without turning. The eigenvalue says by how much. In PCA (principal component analysis), the eigenvectors of the data's covariance matrix are the directions where the data varies most, and the biggest eigenvalues show which directions matter. Keeping the top few lets you squeeze, say, 100 columns into 10 with little information lost. More in Dimensionality reduction.

You don't calculate these by hand. Libraries such as NumPy and PyTorch, running on GPUs, do it. What matters is knowing what each operation means, so the jargon in AI articles makes sense.

Try it yourself

About 10 min

Do linear algebra in a spreadsheet

  1. In Excel or Google Sheets, type 2, 0, 1 in A1:C1 and 4, 0, 2 in A2:C2.
  2. In D1 type =SUMPRODUCT(A1:C1,A2:C2) to get the dot product.
  3. Try =MMULT on two small tables to multiply matrices.

What you should see: The dot product is 10, the same number as the worked example. You've just done the core calculation inside every neuron.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot) where needed. Never paste private or confidential information.

Test what you learned

Take a quick quiz, or look up any word in the glossary.