JOVANA
Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

Multiplying Matrices and Vectors

Matrix times vector is not mysterious: it blends the matrix's columns. Matrix times matrix means "do one transformation, then the other." Along the way we meet the rule that shocks every beginner — order matters, A*B is not B*A — and the matrix that does nothing at all.

Matrix times vector blends the columns

Here is the picture that makes everything click. To compute A times a vector x, take the numbers in x as weights, multiply each column of A by its weight, and add the results. The output is a blend of A's columns.

[[2,0],[0,3]] * (1,1)
  = 1*(2,0) + 1*(0,3)
  = (2,0) + (0,3)
  = (2,3)
x = (1,1) weights column 1 by 1 and column 2 by 1, then adds.

Matrix times matrix = do one, then the other

Matrix times matrix is just this idea, repeated. Reading both matrices as machines, the product A*B means: send a vector through B first, then through A. That chaining is called composition. So A*B applied to x equals A applied to (B applied to x).

  1. Read A*B right-to-left: B happens first, A happens second.
  2. Column j of the product is A applied to column j of B.
  3. Stack those output columns side by side — that is the product matrix.

Shapes must match, and order matters

You can only multiply A*B when A's column count equals B's row count: (m-by-n) times (n-by-p) gives (m-by-p). The two inner numbers must agree and then cancel out. If they do not agree, the product simply does not exist.

A=[[0,-1],[1,0]] (rotate)   B=[[2,0],[0,1]] (stretch)
A*B = [[0,-1],[2, 0]]
B*A = [[0,-2],[1, 0]]   <- different!
Same two matrices, two different products. Order changes the answer.

The matrix that does nothing

The identity matrix I has 1s down the diagonal and 0s everywhere else. Multiplying by it leaves a vector or matrix untouched: I*x = x and A*I = A. It is the number 1 of the matrix world — the do-nothing machine.

[[1,0],[0,1]] * (5,7) = (5,7)   <- unchanged
The identity hands the vector straight back.