Multiplying a matrix by a vector
Read these first if they are new to you
The idea in one paragraph
A matrix is a table of numbers arranged in rows and columns. Multiplying it by a vector gives a new vector. Each number of the new vector is the dot product of one row of the matrix with the input vector, so each is a weighted sum of the inputs. Seen as a picture, the matrix moves every point of the plane in the same way: it can stretch, squash, slant, flip or turn a shape. The columns of the matrix tell you where the two axis arrows land.
Each output number is one row dotted with the vector
A matrix is a rectangle of numbers. One with 2 rows and 3 columns is called a 2 × 3 matrix (say "two by three"). To multiply a matrix A by a vector v, take each row of A in turn and compute its dot product with v. Each dot product gives one number of the result. In the product, the vector is written standing up, as a column, as in the figure below.
The vector must have as many numbers as the matrix has columns, or the dot products do not line up. The result has as many numbers as the matrix has rows. So a 2 × 3 matrix turns 3 numbers into 2.
Multiply A = [[1, 2], [3, 4]] by v = (5, 6). The inner brackets are the rows.
- Row 1 dotted with v: 1 × 5 + 2 × 6 = 5 + 12 = 17.
- Row 2 dotted with v: 3 × 5 + 4 × 6 = 15 + 24 = 39.
- So Av = (17, 39).
Your turn. Multiply [[2, 0], [0, 3]] by (4, 5).
Answer
(2 × 4 + 0 × 5, 0 × 4 + 3 × 5) = (8, 15). This matrix doubles the first number and triples the second.
The columns show where the axis arrows land
There is a second way to read the same sum. Multiply A by (1, 0): the result is (1, 3), the first column of A. Multiply by (0, 1): the result is (2, 4), the second column. Every other vector is a mix of these two arrows. For example, (5, 6) is 5 × (1, 0) + 6 × (0, 1). So the result is the same mix of the columns:
5 × (1, 3) + 6 × (2, 4) = (5 + 12, 15 + 24) = (17, 39)
This is why a matrix moves the whole plane in one consistent way. Once you know where the two axis arrows go, you know where every point goes. Straight lines stay straight, and the origin stays put.
This page covers a matrix times one vector. A matrix times another matrix chains two moves into one, and the
order then matters. Also, much code stores each point as a row of a table, not as a column. It then
writes the product the other way round, as points @ A.T, where A.T is A with
rows and columns swapped. The arithmetic is the same.
mtv.py: @ multiplies a matrix by a vector
import numpy as np
A = np.array([[1, 2],
[3, 4]])
v = np.array([5, 6])
print(A @ v) # [1*5 + 2*6, 3*5 + 4*6]
B = np.array([[1, 0, 2],
[0, 1, -1]]) # 2 rows, 3 columns
w = np.array([1, 2, 3]) # 3 numbers
print(B @ w) # 2 numbers come out
[17 39] [ 7 -1]
Check yourself
-
Where does [[0, −1], [1, 0]] send (1, 0)? Where does it send (0, 1)? What move is this?
Answer
(1, 0) goes to (0, 1), the first column, and (0, 1) goes to (−1, 0), the second column. Both arrows turn a quarter turn counterclockwise, so the matrix turns the whole plane by 90°.
-
A matrix has 3 rows and 5 columns. How many numbers must the input vector have, and how many come out?
Answer
5 go in, one per column. 3 come out, one per row.
-
Which matrix leaves every vector unchanged?
Answer
[[1, 0], [0, 1]]. Its columns are (1, 0) and (0, 1), so the axis arrows stay where they are. It is called the identity matrix.
Go further
Optional background, for readers who want more than this page.
-
The best single explanation of "the columns say where the axis arrows land."
-
Type any four numbers and watch a picture move; good for free play after this page.
Where this shows up
Units that build on this one
- Rotating points with a matrix Basic
- A linear layer: weighted sums Intermediate
- Attention: queries, keys, values Advanced