Skip to main content

Section 1.4 Matrix vector multiplication

We think of a matrix as a rectangular array of numbers. If we say that the shape of a matrix is \(m\times n\text{,}\) we mean that it has \(m\) rows and \(n\) columns. For instance, the shape of the matrix below is \(3\times4\text{:}\)
\begin{equation*} \left[ \begin{array}{rrrr} 0 \amp 4 \amp -3 \amp 1 \\ 3 \amp -1 \amp 2 \amp 0 \\ 2 \amp 0 \amp -1 \amp 1 \\ \end{array} \right]\text{.} \end{equation*}
We may also think of the columns of a matrix as a set of vectors. For instance, the matrix above may be represented as
\begin{equation*} \left[ \begin{array}{rrrr} \vvec_1 \amp \vvec_2 \amp \vvec_3 \amp \vvec_4 \end{array} \right] \end{equation*}
where
\begin{equation*} \vvec_1=\left[\begin{array}{r}0\\3\\2\\ \end{array}\right], \vvec_2=\left[\begin{array}{r}4\\-1\\0\\ \end{array}\right], \vvec_3=\left[\begin{array}{r}-3\\2\\-1\\ \end{array}\right], \vvec_4=\left[\begin{array}{r}1\\0\\1\\ \end{array}\right]\text{.} \end{equation*}
In this way, we see that the \(3\times 4\) matrix is equivalent to an ordered set of 4 vectors in \(\real^3\text{.}\)

Example 1.4.1. Matrix-vector multiplication.

Suppose we have the matrix \(A\) and vector \(\xvec\text{:}\)
\begin{equation*} A = \left[\begin{array}{rr} -2 \amp 3 \\ 0 \amp 2 \\ 3 \amp 1 \\ \end{array}\right],~~~ \xvec = \left[\begin{array}{r} 2 \\ 3 \\ \end{array}\right]\text{.} \end{equation*}
Their product will be defined to be the linear combination of the columns of \(A\) using the components of \(\xvec\) as weights. This means that
\begin{equation*} \begin{aligned} A\xvec = \left[\begin{array}{rr} -2 \amp 3 \\ 0 \amp 2 \\ 3 \amp 1 \\ \end{array}\right] \left[\begin{array}{r} 2 \\ 3 \\ \end{array}\right] {}={} \amp 2 \left[\begin{array}{r} -2 \\ 0 \\ 3 \\ \end{array}\right] + 3 \left[\begin{array}{r} 3 \\ 2 \\ 1 \\ \end{array}\right] \\ \\ {}={} \amp \left[\begin{array}{r} -4 \\ 0 \\ 6 \\ \end{array}\right] + \left[\begin{array}{r} 9 \\ 6 \\ 3 \\ \end{array}\right] \\ \\ {}={} \amp \left[\begin{array}{r} 5 \\ 6 \\ 9 \\ \end{array}\right]. \\ \end{aligned} \end{equation*}
Because \(A\) has two columns, we need two weights to form a linear combination of those columns, which means that \(\xvec\) must have two components. In other words, the number of columns of \(A\) must equal the dimension of the vector \(\xvec\text{.}\)
Similarly, the columns of \(A\) are 3-dimensional so any linear combination of them is 3-dimensional as well. Therefore, \(A\xvec\) will be 3-dimensional.
We then see that if \(A\) is a \(3\times2\) matrix, \(\xvec\) must be a 2-dimensional vector and \(A\xvec\) will be 3-dimensional.

Activity 1.4.1. Matrix-vector multiplication.

  1. Find the matrix product
    \begin{equation*} \left[ \begin{array}{rrrr} 1 \amp 2 \amp 0 \amp -1 \\ 2 \amp 4 \amp -3 \amp -2 \\ -1 \amp -2 \amp 6 \amp 1 \\ \end{array} \right] \left[ \begin{array}{r} 3 \\ 1 \\ -1 \\ 1 \\ \end{array} \right]\text{.} \end{equation*}
  2. Multiplication of a matrix \(A\) and a vector is defined as a linear combination of the columns of \(A\text{.}\) However, there is a shortcut involving dot prducts for computing such a product. Let’s revisit the example where
    \begin{equation*} A = \left[\begin{array}{rr} -2 \amp 3 \\ 0 \amp 2 \\ 3 \amp 1 \\ \end{array}\right],~~~ \xvec = \left[\begin{array}{r} 2 \\ 3 \\ \end{array}\right]\text{.} \end{equation*}
    and focus on the first row of the product.
    \begin{equation*} \left[\begin{array}{rr} -2 \amp 3 \\ 0 \amp 2 \\ 3 \amp 1 \\ \end{array}\right] \left[\begin{array}{r} 2 \\ 3 \\ \end{array}\right] = 2 \left[\begin{array}{r} -2 \\ 0 \\ 3 \\ \end{array}\right] + 3 \left[\begin{array}{r} 3 \\ 2 \\ 1 \\ \end{array}\right] = \left[\begin{array}{c} 2(-2)+3(3) \\2(0)+3(2) \\ 2(3)+3(1) \\ \end{array}\right] = \left[\begin{array}{r}\rvec_1 \cdot \xvec \\ \rvec_2\cdot \xvec \\ \rvec_3 \cdot \xvec \\ \end{array}\right]\text{.} \end{equation*}
    We begin by viewing first row of the matrix \(A\) as a vector; call it \(\rvec_1\text{,}\) hence \(\rvec_1=\twovec{-2}{3}\text{.}\) Next, we compute the dot product \(\rvec_1\cdot \xvec =-2(2)+3(3)\text{.}\) Notice that this dot product is the first component of the matrix vector product.
    Continuing, we view the second row of the matrix as a vector; call it call it \(\rvec_2\text{,}\) hence \(\rvec_2=\twovec{0}{2}\text{.}\) Next, we compute the dot product \(\rvec_2 \cdot \xvec =0(2)+2(3)\text{.}\) Notice that this dot product is the second component of the matrix vector product.
    Lastly we view the third row of the matrix as a vector; call it \(\rvec_3\text{,}\) hence \(\rvec_3=\twovec{3}{1}\text{.}\) In this way, we see that the third component of the product would be obtained computing the dot product \(\rvec_3 \cdot \xvec =3(2)+1(3)\) where
    Evaluate the product in Item a using this shortcut and make sure you end up with the same answer.
  3. Suppose that \(A\) is the matrix
    \begin{equation*} \left[ \begin{array}{rrr} 3 \amp -1 \amp 0 \\ 0 \amp -2 \amp 4 \\ 2 \amp 1 \amp 5 \\ 1 \amp 0 \amp 3 \\ \end{array} \right]\text{.} \end{equation*}
    If \(A\xvec\) is defined, what is the dimension of the vector \(\xvec\) and what is the dimension of \(A\xvec\text{?}\)
  4. A vector whose entries are all zero is denoted by \(\zerovec\text{.}\) If \(A\) is a matrix, what is the product \(A\zerovec\text{?}\)
  5. Suppose that \(I = \left[\begin{array}{rrr} 1 \amp 0 \amp 0 \\ 0 \amp 1 \amp 0 \\ 0 \amp 0 \amp 1 \\ \end{array}\right]\) is the identity matrix and \(\xvec=\threevec{x_1}{x_2}{x_3}\text{.}\) Find the product \(I\xvec\) and explain why \(I\) is called the identity matrix.
  6. Suppose we write the matrix \(A\) in terms of its columns as
    \begin{equation*} A = \left[ \begin{array}{rrrr} \vvec_1 \amp \vvec_2 \amp \cdots \amp \vvec_n \\ \end{array} \right]\text{.} \end{equation*}
    If the vector \(\evec_1 = \left[\begin{array}{c} 1 \\ 0 \\ \vdots \\ 0 \end{array}\right]\text{,}\) what is the product \(A\evec_1\text{?}\)
Answer.
  1. \(\displaystyle \threevec{4}{11}{-10}\text{.}\)
  2. The dimension of \(\xvec\) must three, and the dimension of \(A\xvec\) must be four.
  3. \(A\zerovec = \zerovec\text{.}\)
  4. \(I\xvec = \xvec\text{.}\)
  5. \(A\evec_1 = 1\vvec_1+0\vvec_2+\ldots+0\vvec_n = \vvec_1\text{.}\)
  6. \(\xvec=\twovec{2}{2}\) is the unique solution.
Solution.
  1. We have
    \begin{equation*} \begin{alignedat}{2} \left[\begin{array}{rrrr} 1 \amp 2 \amp 0 \amp -2 \\ 2 \amp 4 \amp -3 \amp -2 \\ -1 \amp -2 \amp 6 \amp 1 \\ \end{array}\right] \amp \fourvec{3}{1}{-1}{1} \\ \amp = \threevec{3(1)+1(2)-1(0)+1(-1)} {3(2)+1(4)-1(-3)+1(-2)} {3(-1)+1(-2)-1(6)+1(1)} \\ \amp =\threevec{4}{11}{-10}\text{.} \end{alignedat} \end{equation*}
  2. The dimension of \(\xvec\) must be the same as the number of columns of \(A\) so \(\xvec\) is three-dimensional. The dimension of \(A\xvec\) equals the number of rows of \(A\) so \(A\xvec\) is four-dimensional.
  3. We have \(A\zerovec = \zerovec\text{.}\)
  4. We have \(I\xvec=\xvec\text{;}\) that is, multiplying a vector by \(I\) produces the same vector.
  5. The product \(A\evec_1 = 1\vvec_1+0\vvec_2+\ldots+0\vvec_n = \vvec_1\text{.}\)
  6. If \(A\xvec=\bvec\text{,}\) then we have
    \begin{equation*} \begin{alignedat}{3} x_1 \amp {}+{} \amp 2x_2 \amp {}={} \amp 6 \\ -x_1 \amp {}+{} \amp x_2 \amp {}={} \amp 0\text{.} \\ \end{alignedat} \end{equation*}
    with corresponding augmented matrix
    \begin{equation*} \left[\begin{array}{rr|r} 1 \amp 2 \amp 6 \\ -1 \amp 1 \amp 0 \end{array}\right] \sim \left[\begin{array}{rr|r} 1 \amp 0 \amp 2 \\ 0 \amp 1 \amp 2 \end{array}\right]\text{.} \end{equation*}
    This means that \(\xvec=\twovec{2}{2}\) is the unique solution to the equation \(A\xvec=\bvec\text{.}\)

Activity 1.4.2.

Sage can find the product of a matrix and vector using the * operator. For example,
  1. Use Sage to evaluate the product
    \begin{equation*} \left[ \begin{array}{rrrr} 1 \amp 2 \amp 0 \amp -1 \\ 2 \amp 4 \amp -3 \amp -2 \\ -1 \amp -2 \amp 6 \amp 1 \\ \end{array} \right] \left[ \begin{array}{r} 3 \\ 1 \\ -1 \\ 1 \\ \end{array} \right] \end{equation*}
  2. In Sage, define the matrix and vectors
    \begin{equation*} A = \left[ \begin{array}{rrr} -2 \amp 0 \\ 3 \amp 1 \\ 4 \amp 2 \\ \end{array} \right], \zerovec = \left[ \begin{array}{r} 0 \\ 0 \end{array} \right], \vvec = \left[ \begin{array}{r} -2 \\ 3 \end{array} \right], \wvec = \left[ \begin{array}{r} 1 \\ 2 \end{array} \right]\text{.} \end{equation*}
  3. What do you find when you evaluate \(A\zerovec\text{?}\)
  4. What do you find when you evaluate \(A(3\vvec)\) and \(3(A\vvec)\) and compare your results?
  5. What do you find when you evaluate \(A(\vvec+\wvec)\) and \(A\vvec + A\wvec\) and compare your results?
Answer.
  1. We define
    A = matrix(3, 4, [1, 2, 0, -1,
                      2, 4, -3, -2,
                      -1, -2, 6, 1])
    v = vector([3, 1, -1, 1])
    A*v		  
    	    
  2. We define
    A = matrix(2, 3, [-2, 0, 3, 1, 4, 2])
    zero = vector([0, 0])
    v = vector([-2, 3])
    w = vector([1, 2])
    	    
  3. \(A\zerovec = \zerovec\text{.}\)
  4. \(A(3\vvec) = 3(A\vvec)\text{.}\)
  5. \(\displaystyle A(\vvec+\wvec) = A\vvec + A\wvec\)
Solution.
  1. We define
    A = matrix(3, 4, [1, 2, 0, -1,
                      2, 4, -3, -2,
                      -1, -2, 6, 1])
    v = vector([3, 1, -1, 1])
    A*v		  
    	    
  2. We define
    A = matrix(2, 3, [-2, 0, 3, 1, 4, 2])
    zero = vector([0, 0])
    v = vector([-2, 3])
    w = vector([1, 2])
    	    
  3. \(A\zerovec = \zerovec\text{.}\)
  4. \(A(3\vvec) = 3(A\vvec)\text{.}\)
  5. \(\displaystyle A(\vvec+\wvec) = A\vvec + A\wvec\)

Activity 1.4.3. Matrix operations.

Since we think of the columns of a matrix as vectors, we define scalar multiplication and matrix addition operations using the corresponding column-wise vector operations. For instance,
\begin{equation*} \begin{aligned} c\left[\begin{array}{rrrr} \vvec_1 \amp \vvec_2 \amp \cdots \amp \vvec_n \end{array} \right] {}={} \amp \left[\begin{array}{rrrr} c\vvec_1 \amp c\vvec_2 \amp \cdots \amp c\vvec_n \end{array} \right] \\ \left[\begin{array}{rrrr} \vvec_1 \amp \vvec_2 \amp \cdots \amp \vvec_n \end{array} \right] {}+{} \amp \left[\begin{array}{rrrr} \wvec_1 \amp \wvec_2 \amp \cdots \amp \wvec_n \end{array} \right] \\ {}={} \amp \left[\begin{array}{rrrr} \vvec_1+\wvec_1 \amp \vvec_2+\wvec_2 \amp \cdots \amp \vvec_n+\wvec_n \end{array} \right]. \\ \end{aligned} \end{equation*}
  1. Compute the scalar multiple
    \begin{equation*} -3\left[ \begin{array}{rrr} 3 \amp 1 \amp 0 \\ -4 \amp 3 \amp -1 \\ \end{array} \right]\text{.} \end{equation*}
  2. Find the sum
    \begin{equation*} \left[ \begin{array}{rr} 0 \amp -3 \\ 1 \amp -2 \\ 3 \amp 4 \\ \end{array} \right] + \left[ \begin{array}{rrr} 4 \amp -1 \\ -2 \amp 2 \\ 1 \amp 1 \\ \end{array} \right]\text{.} \end{equation*}
  3. Suppose that \(A\) and \(B\) are two matrices. What do we need to know about their shapes before we can form the sum \(A+B\text{?}\)
  4. The matrix \(I_n\text{,}\) which we call the identity matrix, is the \(n\times n\) matrix whose entries are zero except for the diagonal entries, all of which are 1. For instance,
    \begin{equation*} I_3 = \left[ \begin{array}{rrr} 1 \amp 0 \amp 0 \\ 0 \amp 1 \amp 0 \\ 0 \amp 0 \amp 1 \\ \end{array} \right]\text{.} \end{equation*}
    If we can form the sum \(A+I_n\text{,}\) what must be true about the matrix \(A\text{?}\)
  5. Find the matrix \(A - 2I_3\) where
    \begin{equation*} A = \left[ \begin{array}{rrr} 1 \amp 2 \amp -2 \\ 2 \amp -3 \amp 3 \\ -2 \amp 3 \amp 4 \\ \end{array} \right]\text{.} \end{equation*}
Answer.
  1. \(\displaystyle \left[\begin{array}{rrr} -9 \amp -3 \amp 0 \\ 12 \amp -9 \amp 3 \\ \end{array}\right]\)
  2. \(\displaystyle \left[\begin{array}{rr} 4 \amp -4 \\ -1 \amp 0 \\ 4 \amp 5 \\ \end{array}\right]\)
  3. The shapes must be the same.
  4. The shape of \(A\) must be \(n\times n\text{.}\)
  5. \(\displaystyle A-2I_3 = \left[\begin{array}{rrr} -1 \amp 2 \amp -2 \\ 2 \amp -5 \amp 3 \\ -2 \amp 3 \amp 2 \\ \end{array}\right]\)
Solution.
  1. \(\displaystyle \left[\begin{array}{rrr} -9 \amp -3 \amp 0 \\ 12 \amp -9 \amp 3 \\ \end{array}\right]\)
  2. \(\displaystyle \left[\begin{array}{rr} 4 \amp -4 \\ -1 \amp 0 \\ 4 \amp 5 \\ \end{array}\right]\)
  3. The shapes must be the same.
  4. The shape of \(A\) must be \(n\times n\text{.}\)
  5. \(\displaystyle A-2I_3 = \left[\begin{array}{rrr} -1 \amp 2 \amp -2 \\ 2 \amp -5 \amp 3 \\ -2 \amp 3 \amp 2 \\ \end{array}\right]\)
As this activity shows, the operations of scalar multiplication and addition of matrices are natural extensions of their vector counterparts. Some care, however, is required when adding matrices. Since we need the same number of vectors to add and since those vectors must be of the same dimension, two matrices must have the same shape if we wish to form their sum.