Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

3.2: Matrix-vector multiplication

The next operation after addition and scalar multiplication is matrix multiplication: how do we multiply matrices to form a product like ABAB? We’ll get there eventually, but first let’s consider a simpler task: multiplying a matrix AA by a vector v⃗\vec v.


Introduction

We’ll start by showing you an example of how to do the calculation. Consider

A=[4−35−267],v⃗=[2−13].A=\begin{bmatrix}4&-3&5\\-2&6&7\end{bmatrix},\qquad \vec v=\begin{bmatrix}{\color{#3d81f6}2}\\{\color{orange}-1}\\{\color{#d81a60}3}\end{bmatrix}.

To multiply AA by v⃗\vec v, multiply corresponding entries in the first row and the vector, then add. Repeat with the second row:

Av⃗=[4(2)+(−3)(−1)+5(3)(−2)(2)+6(−1)+7(3)]=[2611].A\vec v =\begin{bmatrix}4({\color{#3d81f6}2})+(-3)({\color{orange}-1})+5({\color{#d81a60}3})\\(-2)({\color{#3d81f6}2})+6({\color{orange}-1})+7({\color{#d81a60}3})\end{bmatrix} =\begin{bmatrix}26\\11\end{bmatrix}.

What did we just do? There are two interpretations.

Interpretation 1: Dot products of v⃗\vec v with each row of AA

The operations that led us to 26 and 11 look like dot products. Indeed, those values came from dot products, but only if we treat each row as a column vector; you can’t take a dot product between a row vector and a column vector. The two dot products we computed were

[4−35]⋅[2−13]=26,[−267]⋅[2−13]=11.\begin{bmatrix}4\\-3\\5\end{bmatrix}\cdot\begin{bmatrix}2\\-1\\3\end{bmatrix}=26, \qquad \begin{bmatrix}-2\\6\\7\end{bmatrix}\cdot\begin{bmatrix}2\\-1\\3\end{bmatrix}=11.

To take these dot products, we needed the rows of AA to have the same number of entries as v⃗\vec v. Each row of AA had 3 entries, because AA had 3 columns. This tells us our golden rule:

There are dd entries in each row, so the input needs dd entries. There are nn rows, so the output has nn entries. Our first example matrix AA takes a vector in R3\mathbb{R}^3 ([2−13]\begin{bmatrix} 2 \\ -1 \\ 3 \end{bmatrix}) and turns it into a vector in R2\mathbb R^2 ([2611]\begin{bmatrix} 26 \\ 11 \end{bmatrix}).

Interpretation 2: Linear combination of the columns of AA

Let’s look at the computation one more time.

A=[4−35−267],v⃗=[2−13]A=\begin{bmatrix}4&-3&5\\-2&6&7\end{bmatrix},\qquad \vec v=\begin{bmatrix}{\color{#3d81f6}2}\\{\color{orange}-1}\\{\color{#d81a60}3}\end{bmatrix}
Av⃗=[4(2)+(−3)(−1)+5(3)(−2)(2)+6(−1)+7(3)]=[2611].A\vec v =\begin{bmatrix}4({\color{#3d81f6}2})+(-3)({\color{orange}-1})+5({\color{#d81a60}3})\\(-2)({\color{#3d81f6}2})+6({\color{orange}-1})+7({\color{#d81a60}3})\end{bmatrix} =\begin{bmatrix}26\\11\end{bmatrix}.

Let a⃗1,a⃗2,a⃗3\vec a_1,\vec a_2,\vec a_3 denote the columns of AA.

In each row, the first entry is multiplied by 2{\color{#3d81f6}2}, the second by −1{\color{orange}-1}, and the third by 3{\color{#d81a60}3}. Group those contributions vertically:

Av⃗=2[4−2]⏟a⃗1+(−1)[−36]⏟a⃗2+3[57]⏟a⃗3=[8−4]+[3−6]+[1521]=[2611].\begin{aligned} A\vec v &={\color{#3d81f6}2}\underbrace{\begin{bmatrix}4\\-2\end{bmatrix}}_{\vec a_1} +({\color{orange}-1})\underbrace{\begin{bmatrix}-3\\6\end{bmatrix}}_{\vec a_2} +{\color{#d81a60}3}\underbrace{\begin{bmatrix}5\\7\end{bmatrix}}_{\vec a_3}\\ &={\color{#3d81f6}\begin{bmatrix}8\\-4\end{bmatrix}} +{\color{orange}\begin{bmatrix}3\\-6\end{bmatrix}} +{\color{#d81a60}\begin{bmatrix}15\\21\end{bmatrix}} =\begin{bmatrix}26\\11\end{bmatrix}. \end{aligned}

In other words, Av⃗=2a⃗1+(−1)a⃗2+3a⃗3A\vec v={\color{#3d81f6}2}\vec a_1+({\color{orange}-1})\vec a_2+{\color{#d81a60}3}\vec a_3.

So Av⃗A\vec v is a linear combination of the columns of AA! This is an incredibly important interpretation of the operation. The entries of v⃗\vec v are the coefficients.

Click through the figure below to see both interpretations of the same calculation.

Loading...

Another example

Let’s consider another example:

B=[3−48−52−3674].B=\begin{bmatrix}3&-4&8\\-5&2&-3\\6&7&4\end{bmatrix}.

The product of BB with u⃗=[510]\vec u=\begin{bmatrix}{\color{#3d81f6}5}\\{\color{orange}1}\\{\color{#d81a60}0}\end{bmatrix} is

Bu⃗=[3(5)+(−4)(1)+8(0)(−5)(5)+2(1)+(−3)(0)6(5)+7(1)+4(0)]=[11−2337].B\vec u=\begin{bmatrix} 3({\color{#3d81f6}5})+(-4)({\color{orange}1})+8({\color{#d81a60}0})\\ (-5)({\color{#3d81f6}5})+2({\color{orange}1})+(-3)({\color{#d81a60}0})\\ 6({\color{#3d81f6}5})+7({\color{orange}1})+4({\color{#d81a60}0}) \end{bmatrix}=\begin{bmatrix}11\\-23\\37\end{bmatrix}.

As before, write each row of BB as a column vector in R3\mathbb R^3. Its dot product with u⃗\vec u gives the corresponding entry of Bu⃗B\vec u.

We can also use a linear combination of the columns:

Bu⃗=5[3−56]+1[−427]+0[8−34]=[15−2530]+[−427]+[000]=[11−2337].\begin{aligned} B\vec u &={\color{#3d81f6}5}\begin{bmatrix}3\\-5\\6\end{bmatrix} +{\color{orange}1}\begin{bmatrix}-4\\2\\7\end{bmatrix} +{\color{#d81a60}0}\begin{bmatrix}8\\-3\\4\end{bmatrix}\\ &={\color{#3d81f6}\begin{bmatrix}15\\-25\\30\end{bmatrix}} +{\color{orange}\begin{bmatrix}-4\\2\\7\end{bmatrix}} +{\color{#d81a60}\begin{bmatrix}0\\0\\0\end{bmatrix}} =\begin{bmatrix}11\\-23\\37\end{bmatrix}. \end{aligned}

The zero in u⃗\vec u means the third column contributes nothing to this product.


Properties

For an n×dn\times d matrix AA, vectors u⃗,v⃗∈Rd\vec u,\vec v\in\mathbb R^d, and scalars s,ts,t:

  • Distributivity over vector addition: A(u⃗+v⃗)=Au⃗+Av⃗A(\vec u+\vec v)=A\vec u+A\vec v.

  • Compatibility with scaling: A(tv⃗)=t(Av⃗)A(t\vec v)=t(A\vec v).

  • Linearity: A(su⃗+tv⃗)=sAu⃗+tAv⃗A(s\vec u+t\vec v)=sA\vec u+tA\vec v, combining the first two properties.

  • Distributivity over matrix addition: (A+B)v⃗=Av⃗+Bv⃗(A+B)\vec v=A\vec v+B\vec v when BB is also n×dn\times d.

You can check these by expanding the sums in each entry. In particular, A0⃗=0⃗A\vec 0=\vec 0: every matrix sends the zero input to the zero output.

Using linearity

Suppose you already know that

Au⃗=[612],Av⃗=[−15/2−6].A\vec u=\begin{bmatrix}6\\12\end{bmatrix},\qquad A\vec v=\begin{bmatrix}-15/2\\-6\end{bmatrix}.

We can find A(2u⃗−v⃗)A(2\vec u-\vec v) directly from these outputs. By linearity,

A(2u⃗−v⃗)=2Au⃗−Av⃗=2[612]−[−15/2−6]=[39/230].A(2\vec u-\vec v)=2A\vec u-A\vec v =2\begin{bmatrix}6\\12\end{bmatrix} -\begin{bmatrix}-15/2\\-6\end{bmatrix} =\begin{bmatrix}39/2\\30\end{bmatrix}.

The two known outputs are enough; we do not need the entries of AA, u⃗\vec u, or v⃗\vec v.

Order matters!

Av⃗A\vec v is different from v⃗A\vec vA. In fact, the latter is usually not defined! If AA has shape n×dn\times d and v⃗\vec v has shape d×1d\times1, then

A⏟n×d  v⃗⏟d×1=Av⃗⏟n×1.\underbrace{A}_{n\times d}\;\underbrace{\vec v}_{d\times1} =\underbrace{A\vec v}_{n\times1}.

Reversing the order gives

v⃗⏟d×1  A⏟n×d.\underbrace{\vec v}_{d\times1}\;\underbrace{A}_{n\times d}.

Now the inner numbers are 1 and nn, so this product is only defined when n=1n=1. For our first example, Av⃗A\vec v has shapes (2×3)(3×1)(2\times3)(3\times1) and is defined, while v⃗A\vec vA has shapes (3×1)(2×3)(3\times1)(2\times3) and is not.

Even in the special case n=1n=1, the two orders do different things: Av⃗A\vec v has shape 1×11\times1, while v⃗A\vec vA has shape d×dd\times d.


Matrices as operations

When we multiply AA by v⃗\vec v, what comes out is a new vector, Av⃗A\vec v. We should think of the process of multiplying v⃗\vec v by AA as transforming v⃗\vec v from one vector to another.

v⃗⏟input  ⟶  Multiply by A  ⟶  Av⃗⏟output.\underbrace{\vec v}_{\text{input}} \;\longrightarrow\;\boxed{\text{Multiply by }A}\;\longrightarrow\; \underbrace{A\vec v}_{\text{output}}.

Let’s try and understand the various ways a matrix AA can transform a vector v⃗\vec v. If AA has shape n×dn\times d, the input has shape d×1d\times1 and the output has shape n×1n\times1. The input and output don’t even need to have the same number of entries!

For example, consider the 2×52\times5 matrix

A=[120−130−1211].A=\begin{bmatrix}1&2&0&-1&3\\0&-1&2&1&1\end{bmatrix}.

Multiplication by AA takes a vector in R5\mathbb R^5 and produces a vector in R2\mathbb R^2. Here is one input and its output:

[12345]⏟v⃗ ∈ R5  ⟶  Multiply by A  ⟶  [1613]⏟Av⃗ ∈ R2.\underbrace{\begin{bmatrix}1\\2\\3\\4\\5\end{bmatrix}}_{\vec v\,\in\,\mathbb R^5} \;\longrightarrow\;\boxed{\text{Multiply by }A}\;\longrightarrow\; \underbrace{\begin{bmatrix}16\\13\end{bmatrix}}_{A\vec v\,\in\,\mathbb R^2}.

The two rows of AA give the two output entries:

Av⃗=[1(1)+2(2)+0(3)−1(4)+3(5)0(1)−1(2)+2(3)+1(4)+1(5)]=[1613].A\vec v=\begin{bmatrix}1(1)+2(2)+0(3)-1(4)+3(5)\\0(1)-1(2)+2(3)+1(4)+1(5)\end{bmatrix} =\begin{bmatrix}16\\13\end{bmatrix}.

The input has shape 5×15\times1, and the output has shape 2×12\times1.

For the next five examples, we’ll use 2×22\times2 matrices. Then both the input and output have shape 2×12\times1, so both are vectors in R2\mathbb R^2. This lets us draw the transformation on a coordinate plane and see what it does.

We’ll use the same two input vectors in each example:

u⃗=[−46],v⃗=[5−3].{\color{orange}\vec u=\begin{bmatrix}-4\\6\end{bmatrix}},\qquad {\color{#3d81f6}\vec v=\begin{bmatrix}5\\-3\end{bmatrix}}.

In each pair of graphs, the inputs appear on the left and the outputs appear on the right, with the same axes and scale. Orange identifies u⃗\vec u and Au⃗A\vec u; blue identifies v⃗\vec v and Av⃗A\vec v. We’ll also multiply AA by an arbitrary input [ab]\begin{bmatrix}a\\b\end{bmatrix} to understand what happens to every vector.

Example 1: The identity matrix

A=[1001],u⃗=[−46],v⃗=[5−3].A=\begin{bmatrix}1&0\\0&1\end{bmatrix},\qquad\vec u=\begin{bmatrix}-4\\6\end{bmatrix},\qquad\vec v=\begin{bmatrix}5\\-3\end{bmatrix}.

Multiplying by each input gives

Au⃗=[1(−4)+0(6)0(−4)+1(6)]=[−46],A\vec u=\begin{bmatrix}1(-4)+0(6)\\0(-4)+1(6)\end{bmatrix}=\begin{bmatrix}-4\\6\end{bmatrix},
Av⃗=[1(5)+0(−3)0(5)+1(−3)]=[5−3].A\vec v=\begin{bmatrix}1(5)+0(-3)\\0(5)+1(-3)\end{bmatrix}=\begin{bmatrix}5\\-3\end{bmatrix}.
Loading...

This matrix leaves every vector unchanged.

A[ab]=[ab].A\begin{bmatrix}a\\b\end{bmatrix}=\begin{bmatrix}a\\b\end{bmatrix}.

Example 2: A diagonal matrix

A=[−3/2002],u⃗=[−46],v⃗=[5−3].A=\begin{bmatrix}-3/2&0\\0&2\end{bmatrix},\qquad\vec u=\begin{bmatrix}-4\\6\end{bmatrix},\qquad\vec v=\begin{bmatrix}5\\-3\end{bmatrix}.

Multiplying by each input gives

Au⃗=[(−3/2)(−4)+0(6)0(−4)+2(6)]=[612],A\vec u=\begin{bmatrix}(-3/2)(-4)+0(6)\\0(-4)+2(6)\end{bmatrix}=\begin{bmatrix}6\\12\end{bmatrix},
Av⃗=[(−3/2)(5)+0(−3)0(5)+2(−3)]=[−15/2−6].A\vec v=\begin{bmatrix}(-3/2)(5)+0(-3)\\0(5)+2(-3)\end{bmatrix}=\begin{bmatrix}-15/2\\-6\end{bmatrix}.
Loading...

The key here is that a diagonal matrix transforms each component of the input vector independently. This matrix multiplies the first component by −3/2-3/2 and the second by 2. Each output component depends only on the corresponding input component; the components do not get mixed together.

A[ab]=[−3a/22b].A\begin{bmatrix}a\\b\end{bmatrix}=\begin{bmatrix}-3a/2\\2b\end{bmatrix}.

Example 3: A permutation matrix

A=[0110],u⃗=[−46],v⃗=[5−3].A=\begin{bmatrix}0&1\\1&0\end{bmatrix},\qquad\vec u=\begin{bmatrix}-4\\6\end{bmatrix},\qquad\vec v=\begin{bmatrix}5\\-3\end{bmatrix}.

Multiplying by each input gives

Au⃗=[0(−4)+1(6)1(−4)+0(6)]=[6−4],A\vec u=\begin{bmatrix}0(-4)+1(6)\\1(-4)+0(6)\end{bmatrix}=\begin{bmatrix}6\\-4\end{bmatrix},
Av⃗=[0(5)+1(−3)1(5)+0(−3)]=[−35].A\vec v=\begin{bmatrix}0(5)+1(-3)\\1(5)+0(-3)\end{bmatrix}=\begin{bmatrix}-3\\5\end{bmatrix}.
Loading...

The two coordinates swap. Geometrically, this reflects vectors across the line with equation x2=x1x_2=x_1.

A[ab]=[ba].A\begin{bmatrix}a\\b\end{bmatrix}=\begin{bmatrix}b\\a\end{bmatrix}.

Example 4: Sending vectors onto a line

A=[36−2−4],u⃗=[−46],v⃗=[5−3].A=\begin{bmatrix}3&6\\-2&-4\end{bmatrix},\qquad\vec u=\begin{bmatrix}-4\\6\end{bmatrix},\qquad\vec v=\begin{bmatrix}5\\-3\end{bmatrix}.

Multiplying by each input gives

Au⃗=[3(−4)+6(6)(−2)(−4)+(−4)(6)]=[24−16],A\vec u=\begin{bmatrix}3(-4)+6(6)\\(-2)(-4)+(-4)(6)\end{bmatrix}=\begin{bmatrix}24\\-16\end{bmatrix},
Av⃗=[3(5)+6(−3)(−2)(5)+(−4)(−3)]=[−32].A\vec v=\begin{bmatrix}3(5)+6(-3)\\(-2)(5)+(-4)(-3)\end{bmatrix}=\begin{bmatrix}-3\\2\end{bmatrix}.
Loading...

Even though the input vectors u⃗\vec u and v⃗\vec v do not lie on the same line through the origin, the output vectors Au⃗A\vec u and Av⃗A\vec v do! Multiplication by this matrix sends them onto the same line. Every output is a multiple of [3−2]\begin{bmatrix}3\\-2\end{bmatrix}.

A[ab]=[3a+6b−2a−4b]=(a+2b)[3−2].A\begin{bmatrix}a\\b\end{bmatrix}=\begin{bmatrix}3a+6b\\-2a-4b\end{bmatrix}=(a+2b)\begin{bmatrix}3\\-2\end{bmatrix}.

Example 5: A rotation matrix

A=[3/5−4/54/53/5],u⃗=[−46],v⃗=[5−3].A=\begin{bmatrix}3/5&-4/5\\4/5&3/5\end{bmatrix},\qquad\vec u=\begin{bmatrix}-4\\6\end{bmatrix},\qquad\vec v=\begin{bmatrix}5\\-3\end{bmatrix}.

Multiplying by each input gives

Au⃗=[(3/5)(−4)−(4/5)(6)(4/5)(−4)+(3/5)(6)]=[−36/52/5],A\vec u=\begin{bmatrix}(3/5)(-4)-(4/5)(6)\\(4/5)(-4)+(3/5)(6)\end{bmatrix}=\begin{bmatrix}-36/5\\2/5\end{bmatrix},
Av⃗=[(3/5)(5)−(4/5)(−3)(4/5)(5)+(3/5)(−3)]=[27/511/5].A\vec v=\begin{bmatrix}(3/5)(5)-(4/5)(-3)\\(4/5)(5)+(3/5)(-3)\end{bmatrix}=\begin{bmatrix}27/5\\11/5\end{bmatrix}.
Loading...

This matrix rotates every vector counterclockwise by the same angle, approximately 53.1∘53.1^\circ, while preserving its length.

A[ab]=[(3a−4b)/5(4a+3b)/5].A\begin{bmatrix}a\\b\end{bmatrix}=\begin{bmatrix}(3a-4b)/5\\(4a+3b)/5\end{bmatrix}.

The angle θ\theta satisfies cos⁡θ=3/5\cos\theta=3/5 and sin⁡θ=4/5\sin\theta=4/5.

You’ll explore this more in Lab 6.

Comparing the five operations

ExampleMatrixOperation
1[1001]\begin{bmatrix}1&0\\0&1\end{bmatrix}Identity: leaves every vector unchanged.
2[−3/2002]\begin{bmatrix}-3/2&0\\0&2\end{bmatrix}Diagonal: scales the first coordinate by −3/2-3/2 and the second by 2.
3[0110]\begin{bmatrix}0&1\\1&0\end{bmatrix}Permutation: swaps the coordinates, reflecting across x2=x1x_2=x_1.
4[36−2−4]\begin{bmatrix}3&6\\-2&-4\end{bmatrix}Sends every vector onto the line 2x1+3x2=02x_1+3x_2=0.
5[3/5−4/54/53/5]\begin{bmatrix}3/5&-4/5\\4/5&3/5\end{bmatrix}Rotation: turns every vector counterclockwise by about 53.1∘53.1^\circ, preserving its length.

Moving forward

There are two questions we will explore over the coming weeks.

  1. Given AA and v⃗\vec v, what is b⃗=Av⃗\vec b=A\vec v? Compute the output and describe the operation that AA applies to v⃗\vec v. The five examples above illustrate some possible operations: leaving vectors unchanged, scaling coordinates, swapping coordinates, sending vectors onto a line, and rotating vectors.

  2. Given AA and b⃗\vec b, can we find v⃗\vec v such that Av⃗=b⃗A\vec v=\vec b? This asks us to work backward from a desired output.

The second question need not have an answer. For the matrix in Example 4,

A[ab]=(a+2b)[3−2].A\begin{bmatrix}a\\b\end{bmatrix}=(a+2b)\begin{bmatrix}3\\-2\end{bmatrix}.

The target [24−16]\begin{bmatrix}24\\-16\end{bmatrix} is reachable: the input [−46]\begin{bmatrix}-4\\6\end{bmatrix} produces it. But [24−15]\begin{bmatrix}24\\-15\end{bmatrix} is unreachable, because every output satisfies 2x+3y=02x+3y=0. No input can make Av⃗A\vec v leave that line.

Even when an input exists, it need not be unique: [80]\begin{bmatrix}8\\0\end{bmatrix} also produces [24−16]\begin{bmatrix}24\\-16\end{bmatrix}.


Recap: Two interpretations of the same product

There are two ways to understand Av⃗A\vec v: dot products with the rows, written as column vectors, and a linear combination of the columns. They always give the same result. Let’s see both with a new example:

A=[2−134−25],v⃗=[3−2].A=\begin{bmatrix}2&-1\\3&4\\-2&5\end{bmatrix},\qquad \vec v=\begin{bmatrix}3\\-2\end{bmatrix}.

The shapes are (3×2)(2×1)(3\times2)(2\times1), so the output has shape 3×13\times1.

Dot products with the rows

Write each row as a column vector and take its dot product with v⃗\vec v:

[2−1]⋅[3−2]=8,[34]⋅[3−2]=1,[−25]⋅[3−2]=−16.\begin{bmatrix}2\\-1\end{bmatrix}\cdot\begin{bmatrix}3\\-2\end{bmatrix}=8,\qquad \begin{bmatrix}3\\4\end{bmatrix}\cdot\begin{bmatrix}3\\-2\end{bmatrix}=1,\qquad \begin{bmatrix}-2\\5\end{bmatrix}\cdot\begin{bmatrix}3\\-2\end{bmatrix}=-16.

These three numbers are the entries of the output:

Av⃗=[2(3)+(−1)(−2)3(3)+4(−2)(−2)(3)+5(−2)]=[81−16].A\vec v=\begin{bmatrix}2(3)+(-1)(-2)\\3(3)+4(-2)\\(-2)(3)+5(-2)\end{bmatrix} =\begin{bmatrix}8\\1\\-16\end{bmatrix}.

A linear combination of the columns

The entries of v⃗\vec v tell us to multiply the first column by 3 and the second column by -2, then add:

Av⃗=3[23−2]+(−2)[−145]=[69−6]+[2−8−10]=[81−16].A\vec v=3\begin{bmatrix}2\\3\\-2\end{bmatrix} +(-2)\begin{bmatrix}-1\\4\\5\end{bmatrix} =\begin{bmatrix}6\\9\\-6\end{bmatrix}+\begin{bmatrix}2\\-8\\-10\end{bmatrix} =\begin{bmatrix}8\\1\\-16\end{bmatrix}.

The row interpretation computes the output one entry at a time. The column interpretation builds the output as a sum of vectors. Both describe the same operation!