Matrix-Vector Products

A matrix-vector product uses one dot product per row.

Let:

A=[2113]A = \begin{bmatrix} 2 & 1 \\ -1 & 3 \end{bmatrix}

and:

x=[45]x = \begin{bmatrix} 4 \\ 5 \end{bmatrix}

Then:

Ax=[24+1514+35]=[1311]Ax = \begin{bmatrix} 2 \cdot 4 + 1 \cdot 5 \\ -1 \cdot 4 + 3 \cdot 5 \end{bmatrix} = \begin{bmatrix} 13 \\ 11 \end{bmatrix}
21-13matrix A45vector xrow dot products1311output Ax[2, 1] · [4, 5] = 13
A matrix-vector product is one dot product per row.

Read The Rows

The first output entry comes from the first row:

[2,1][4,5]=13[2, 1] \cdot [4, 5] = 13

The second output entry comes from the second row:

[1,3][4,5]=11[-1, 3] \cdot [4, 5] = 11

That is the whole operation. Each row asks one question about the input vector.

This is the most useful intuition on the page:

one row = one weighted question
one output entry
= the answer to that question

If a row is [2, 1], it asks for "two parts of the first input coordinate plus one part of the second." If another row is [-1, 3], it asks a different weighted question about the same input.

MATH-C03-T04-001Exercise: Compute the first output

For the same AA and xx, what is the first entry of AxAx?

Compute it first, then check your number.

HintUse the first row

Compute [2,1][4,5][2, 1] \cdot [4, 5].

SolutionFirst row dot product
[2,1][4,5]=24+15=13[2, 1] \cdot [4, 5] = 2 \cdot 4 + 1 \cdot 5 = 13

The first row produces the first output entry. It asks for two parts of the first input coordinate and one part of the second.

MATH-C03-T04-002Exercise: Compute the second output

What is the second entry of AxAx for the same matrix and vector?

Compute it first, then check your number.

HintUse the second row

Compute [1,3][4,5][-1,3]\cdot[4,5].

SolutionSecond row dot product
[1,3][4,5]=(1)4+35=4+15=11[-1,3]\cdot[4,5] = (-1)\cdot4 + 3\cdot5 = -4 + 15 = 11

The second row produces the second output entry. It subtracts one copy of the first coordinate and adds three copies of the second.

MATH-C03-T04-005Exercise: Rows as questions

Enter 1 if a 5 x 3 matrix asks five row-wise questions of a length-3 input vector.

Compute it first, then check your number.

HintOne row, one output

A 5 x 3 matrix has five rows.

SolutionFive outputs

Enter 1. Each row has three entries, so it can take a dot product with a length-3 input. Five rows produce five output entries.

Shape Of The Product

The matrix has shape 2 x 2. The vector has length 2. The product has length 2.

In general:

(m x n) matrix
times length-n vector
-> length-m vector

This is why the output has one entry per row.

MATH-C03-T04-003Exercise: Predict matrix-vector shape

A matrix has shape 3 x 2, and xx has length 2. What is the length of AxAx?

Compute it first, then check your number.

HintOne output per row

A 3 x 2 matrix has three rows.

SolutionOutput length

The matrix has shape 3 x 2, and the vector has length 2, so the inner dimension fits. The output length is 3.

Code Mirror

In NumPy, the @ operator performs the matrix-vector product:

import numpy as np

A = np.array([[2, 1], [-1, 3]])
x = np.array([4, 5])

print(A @ x)

The output is:

[13 11]
MATH-C03-T04-004Exercise: Trace the code

In the code above, enter 1 if A @ x means one dot product per row of A.

Compute it first, then check your number.

HintRead @ as matrix product

For a matrix and vector, @ computes the matrix-vector product.

SolutionRows produce outputs

Enter 1. Each row of AA takes a dot product with xx, producing one output entry.

Next, we multiply matrices by reading columns as inputs.