Matrix-Vector Products
A matrix-vector product uses one dot product per row.
Let:
and:
Then:
Read The Rows
The first output entry comes from the first row:
The second output entry comes from the second row:
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.
For the same and , what is the first entry of ?
Compute it first, then check your number.
HintUse the first row
Compute .
SolutionFirst row dot product
The first row produces the first output entry. It asks for two parts of the first input coordinate and one part of the second.
What is the second entry of for the same matrix and vector?
Compute it first, then check your number.
HintUse the second row
Compute .
SolutionSecond row dot product
The second row produces the second output entry. It subtracts one copy of the first coordinate and adds three copies of the second.
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.
A matrix has shape 3 x 2, and has length 2. What is the length of
?
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]
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 takes a dot product with , producing one
output entry.
Next, we multiply matrices by reading columns as inputs.