Exercise: matrix times vector

Goal

  • Multiply a matrix by a vector by hand, one row at a time.
  • See each result entry as a dot product of a matrix row with the vector.
  • Check the shape rule: the number of columns of \(A\) must equal the length of \(\mathbf{x}\).

The rule

We want to compute \(A\mathbf{x}\), where \(A\) is a matrix and \(\mathbf{x}\) is a column vector.

Shape rule first. If \(A\) has size \(m \times n\), then \(\mathbf{x}\) must have length \(n\) (it must be \(n \times 1\)). The number of columns of \(A\) has to match the length of \(\mathbf{x}\). The answer is a column vector of length \(m\) (one number per row of \(A\)).

How to compute it. Each entry of the answer comes from one row of \(A\) combined with the vector \(\mathbf{x}\). You walk along the row and the vector together, multiply matching numbers, and add them up. This is called a dot product.

Here is a tiny worked example with a \(2 \times 2\) matrix: \[ A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \qquad \mathbf{x} = \begin{bmatrix} 5 \\ 6 \end{bmatrix}. \]

  • Row 1 of \(A\) is \(\begin{bmatrix} 1 & 2 \end{bmatrix}\). Combine with \(\mathbf{x}\): \[ (1)(5) + (2)(6) = 5 + 12 = 17. \]
  • Row 2 of \(A\) is \(\begin{bmatrix} 3 & 4 \end{bmatrix}\). Combine with \(\mathbf{x}\): \[ (3)(5) + (4)(6) = 15 + 24 = 39. \]

Stack the two results into a column: \[ A\mathbf{x} = \begin{bmatrix} (1)(5)+(2)(6) \\ (3)(5)+(4)(6) \end{bmatrix} = \begin{bmatrix} 17 \\ 39 \end{bmatrix}. \]

That is the whole idea: one row at a time, multiply across, add up.

Problems

Start with \(2 \times 2\) times a length-2 vector.

1. \(\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 4 \\ 7 \end{bmatrix}\)

2. \(\begin{bmatrix} 2 & 0 \\ 0 & 3 \end{bmatrix} \begin{bmatrix} 5 \\ 1 \end{bmatrix}\)

3. \(\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \end{bmatrix}\)

4. \(\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 1 \\ 1 \end{bmatrix}\)

5. \(\begin{bmatrix} 2 & 1 \\ 0 & 5 \end{bmatrix} \begin{bmatrix} 3 \\ 2 \end{bmatrix}\)

6. \(\begin{bmatrix} 4 & 1 \\ 2 & 3 \end{bmatrix} \begin{bmatrix} 2 \\ 1 \end{bmatrix}\)

7. \(\begin{bmatrix} 1 & 2 \\ 2 & 1 \end{bmatrix} \begin{bmatrix} 3 \\ 4 \end{bmatrix}\)

8. \(\begin{bmatrix} 5 & 0 \\ 1 & 2 \end{bmatrix} \begin{bmatrix} 2 \\ 6 \end{bmatrix}\)

9. \(\begin{bmatrix} 3 & 2 \\ 1 & 4 \end{bmatrix} \begin{bmatrix} 0 \\ 5 \end{bmatrix}\)

10. \(\begin{bmatrix} 2 & 3 \\ 4 & 1 \end{bmatrix} \begin{bmatrix} 1 \\ 2 \end{bmatrix}\)

Now a couple where you should check the shape first.

11. Can you compute \(\begin{bmatrix} 1 & 2 & 3 \end{bmatrix} \begin{bmatrix} 2 \\ 1 \end{bmatrix}\)? If yes, do it; if no, explain why.

12. Can you compute \(\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 5 \\ 6 \\ 7 \end{bmatrix}\)? If yes, do it; if no, explain why.

Now \(3 \times 3\) times a length-3 vector.

13. \(\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix}\)

14. \(\begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \\ 1 \end{bmatrix}\)

15. \(\begin{bmatrix} 1 & 2 & 0 \\ 0 & 1 & 3 \\ 1 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 \\ 2 \\ 1 \end{bmatrix}\)

16. \(\begin{bmatrix} 2 & 0 & 1 \\ 1 & 3 & 0 \\ 0 & 1 & 2 \end{bmatrix} \begin{bmatrix} 1 \\ 1 \\ 2 \end{bmatrix}\)

Hints

The answer has one entry per row of \(A\). Do row 1 to get the first answer entry, row 2 to get the second, and so on.

For a row, multiply its first number by the vector’s first number, its second by the vector’s second, and so on, then add all those products together.

Columns of \(A\) must equal the length of \(\mathbf{x}\). If a \(1\times 3\) row meets a length-2 vector, the count does not match, so the product does not exist.

Multiplying by the identity matrix gives back the same vector. Multiplying by a diagonal matrix just scales each entry of the vector.

Step-by-step solutions

1. Identity times the vector returns it unchanged. Row 1: \((1)(4)+(0)(7)=4\). Row 2: \((0)(4)+(1)(7)=7\). \(\Rightarrow \begin{bmatrix} 4 \\ 7 \end{bmatrix}\).

2. Row 1: \((2)(5)+(0)(1)=10\). Row 2: \((0)(5)+(3)(1)=3\). \(\Rightarrow \begin{bmatrix} 10 \\ 3 \end{bmatrix}\).

3. Row 1: \((1)(2)+(1)(3)=5\). Row 2: \((1)(2)+(1)(3)=5\). \(\Rightarrow \begin{bmatrix} 5 \\ 5 \end{bmatrix}\).

4. Row 1: \((1)(1)+(2)(1)=3\). Row 2: \((3)(1)+(4)(1)=7\). \(\Rightarrow \begin{bmatrix} 3 \\ 7 \end{bmatrix}\).

5. Row 1: \((2)(3)+(1)(2)=6+2=8\). Row 2: \((0)(3)+(5)(2)=0+10=10\). \(\Rightarrow \begin{bmatrix} 8 \\ 10 \end{bmatrix}\).

6. Row 1: \((4)(2)+(1)(1)=8+1=9\). Row 2: \((2)(2)+(3)(1)=4+3=7\). \(\Rightarrow \begin{bmatrix} 9 \\ 7 \end{bmatrix}\).

7. Row 1: \((1)(3)+(2)(4)=3+8=11\). Row 2: \((2)(3)+(1)(4)=6+4=10\). \(\Rightarrow \begin{bmatrix} 11 \\ 10 \end{bmatrix}\).

8. Row 1: \((5)(2)+(0)(6)=10+0=10\). Row 2: \((1)(2)+(2)(6)=2+12=14\). \(\Rightarrow \begin{bmatrix} 10 \\ 14 \end{bmatrix}\).

9. Row 1: \((3)(0)+(2)(5)=0+10=10\). Row 2: \((1)(0)+(4)(5)=0+20=20\). \(\Rightarrow \begin{bmatrix} 10 \\ 20 \end{bmatrix}\).

10. Row 1: \((2)(1)+(3)(2)=2+6=8\). Row 2: \((4)(1)+(1)(2)=4+2=6\). \(\Rightarrow \begin{bmatrix} 8 \\ 6 \end{bmatrix}\).

11. Shape check: the row \(\begin{bmatrix} 1 & 2 & 3 \end{bmatrix}\) has \(3\) columns, but the vector has length \(2\). They do not match, so the product cannot be computed.

12. Shape check: the matrix has \(2\) columns, but the vector has length \(3\). They do not match, so the product cannot be computed.

13. Identity times the vector returns it unchanged. Row 1: \((1)(4)+(0)(5)+(0)(6)=4\). Row 2: \((0)(4)+(1)(5)+(0)(6)=5\). Row 3: \((0)(4)+(0)(5)+(1)(6)=6\). \(\Rightarrow \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix}\).

14. Every row is all ones, so each entry is just the sum \(2+3+1=6\). Row 1: \((1)(2)+(1)(3)+(1)(1)=6\). Same for rows 2 and 3. \(\Rightarrow \begin{bmatrix} 6 \\ 6 \\ 6 \end{bmatrix}\).

15. Row 1: \((1)(1)+(2)(2)+(0)(1)=1+4+0=5\). Row 2: \((0)(1)+(1)(2)+(3)(1)=0+2+3=5\). Row 3: \((1)(1)+(0)(2)+(1)(1)=1+0+1=2\). \(\Rightarrow \begin{bmatrix} 5 \\ 5 \\ 2 \end{bmatrix}\).

16. Row 1: \((2)(1)+(0)(1)+(1)(2)=2+0+2=4\). Row 2: \((1)(1)+(3)(1)+(0)(2)=1+3+0=4\). Row 3: \((0)(1)+(1)(1)+(2)(2)=0+1+4=5\). \(\Rightarrow \begin{bmatrix} 4 \\ 4 \\ 5 \end{bmatrix}\).