Unit 1, lesson 3

Matrices and Matrix-Vector Multiplication

What an m by n matrix is, how to read its size and entries, how to compute a matrix-vector product as a weighted sum of columns or row by row, and how matrix addition, scalar multiples and the transpose work.

  • Video 3
  • Matrices and Linear Transformations

A gift shop packs two kinds of boxes. A small box holds 2 candles and 1 card, and a large box holds 3 candles, 1 card and 2 bars of soap. How many candles, cards and soaps does an order of 4 small boxes and 1 large box need, and how can the count be redone quickly whenever the order changes? Writing the contents of the two boxes as the columns of one array, a matrix, makes every such count a single product of the matrix with the order. This lesson defines matrices and that product, and then matrix addition, scalar multiples and the transpose, each through the entries.

Key terms

Matrix

Definition Let mm and nn be positive integers. An m×nm \times n matrix is a rectangular array of real numbers with mm rows and nn columns. Its size is m×nm \times n, read as mm by nn, with the number of rows first. The entry in row ii and column jj of a matrix AA is written aija_{ij}, again row first, and we write A=[aij]A = [a_{ij}]. Column jj of AA is a vector aj\mathbf{a}_j in Rm\mathbb{R}^m, and listing the columns gives

A=[a1a2⋯an].A = \begin{bmatrix} \mathbf{a}_1 & \mathbf{a}_2 & \cdots & \mathbf{a}_n \end{bmatrix}.

Two matrices are equal when they have the same size and the same entry in every position. A matrix with as many rows as columns is square. In running text a tuple such as (2,1,0)(2, 1, 0) stands for a column vector.

In plain words Rows run across and columns run down. The size and the label of an entry both name the row first and the column second.

Example The gift shop’s boxes give the 3×23 \times 2 matrix

G=[231102].G = \begin{bmatrix} 2 & 3 \\ 1 & 1 \\ 0 & 2 \end{bmatrix}.

Its rows count candles, cards and soaps, in that order, and its columns are the small box g1=(2,1,0)\mathbf{g}_1 = (2, 1, 0) and the large box g2=(3,1,2)\mathbf{g}_2 = (3, 1, 2). The entry g32=2g_{32} = 2 says that a large box holds two soaps, and g31=0g_{31} = 0 says that a small box holds none. Keep the notation straight. The number g21=1g_{21} = 1 is a single entry, while the bold g1\mathbf{g}_1 is a whole column.

Matrix-vector product

Definition Let AA be an m×nm \times n matrix with columns a1,…,an\mathbf{a}_1, \dots, \mathbf{a}_n, and let x=(x1,…,xn)\mathbf{x} = (x_1, \dots, x_n) be a vector in Rn\mathbb{R}^n. The matrix-vector product AxA\mathbf{x} is the weighted sum of the columns of AA with the entries of x\mathbf{x} as weights,

Ax=x1a1+x2a2+⋯+xnan.A\mathbf{x} = x_1\mathbf{a}_1 + x_2\mathbf{a}_2 + \cdots + x_n\mathbf{a}_n.

In other words, AxA\mathbf{x} is the linear combination of the columns of AA whose coefficients are the entries of x\mathbf{x}. The product is defined only when x\mathbf{x} has exactly nn entries, one weight for each column. Since every column lies in Rm\mathbb{R}^m, so does AxA\mathbf{x}. An m×nm \times n matrix therefore takes inputs in Rn\mathbb{R}^n and gives outputs in Rm\mathbb{R}^m, and we say that AA maps Rn\mathbb{R}^n to Rm\mathbb{R}^m.

In plain words Each entry of the input says how much of the matching column to take. Count the columns to see which inputs are allowed and the rows to see how long the answer is.

Example The order of 4 small boxes and 1 large box is x=(4,1)\mathbf{x} = (4, 1), and

Gx=4[210]+1[312]=[1152].G\mathbf{x} = 4\begin{bmatrix} 2 \\ 1 \\ 0 \end{bmatrix} + 1\begin{bmatrix} 3 \\ 1 \\ 2 \end{bmatrix} = \begin{bmatrix} 11 \\ 5 \\ 2 \end{bmatrix}.

The order needs 11 candles, 5 cards and 2 soaps. A new order changes only x\mathbf{x}, never the matrix. The matrix GG maps R2\mathbb{R}^2 to R3\mathbb{R}^3, so a product such as GG times (4,1,0)(4, 1, 0) is not defined, since GG has only two columns to weight.

Row-by-row calculation

Definition For an m×nm \times n matrix A=[aij]A = [a_{ij}] and a vector x\mathbf{x} in Rn\mathbb{R}^n, entry ii of AxA\mathbf{x} is

(Ax)i=ai1x1+⋯+ainxn=∑j=1naijxj\begin{aligned} (A\mathbf{x})_i &= a_{i1}x_1 + \cdots + a_{in}x_n \\[0.4em] &= \sum_{j=1}^{n} a_{ij}x_j \end{aligned}

for i=1,…,mi = 1, \dots, m. This follows from the column definition, because entry ii of xjajx_j\mathbf{a}_j is aijxja_{ij}x_j and a sum of vectors is added one position at a time. The summation sign adds the terms for j=1,…,nj = 1, \dots, n. The row index ii stays fixed while jj runs across row ii.

In plain words To get one output entry, run along that row of the matrix and down the input at the same time, multiply matching entries and add.

Example Let

M=[3−124],x=[2−3].M = \begin{bmatrix} 3 & -1 \\ 2 & 4 \end{bmatrix}, \qquad \mathbf{x} = \begin{bmatrix} 2 \\ -3 \end{bmatrix}.

Row by row, 3⋅2+(−1)(−3)=6+3=93 \cdot 2 + (-1)(-3) = 6 + 3 = 9 and 2⋅2+4⋅(−3)=4−12=−82 \cdot 2 + 4 \cdot (-3) = 4 - 12 = -8, so Mx=(9,−8)M\mathbf{x} = (9, -8). The columns confirm it, since 2(3,2)−3(−1,4)=(6,4)+(3,−12)=(9,−8)2(3, 2) - 3(-1, 4) = (6, 4) + (3, -12) = (9, -8). A first entry of 33 would mean the product (−1)(−3)(-1)(-3) was taken as −3-3, and comparing with the column calculation exposes that slip.

Matrix addition

Definition Let A=[aij]A = [a_{ij}] and B=[bij]B = [b_{ij}] be matrices of the same size m×nm \times n. Their sum A+BA + B is the m×nm \times n matrix with entries

(A+B)ij=aij+bij.(A + B)_{ij} = a_{ij} + b_{ij}.

Matrices of different sizes cannot be added. For every x\mathbf{x} in Rn\mathbb{R}^n the sum satisfies (A+B)x=Ax+Bx(A + B)\mathbf{x} = A\mathbf{x} + B\mathbf{x}, because in each row the entry aij+bija_{ij} + b_{ij} multiplies xjx_j, and the products split and regroup into the row calculations for AxA\mathbf{x} and BxB\mathbf{x}.

In plain words Add matching entries. Running both matrices on the same input and adding the outputs gives the same answer as running their sum.

Example For

A=[1−203],B=[41−12],A = \begin{bmatrix} 1 & -2 \\ 0 & 3 \end{bmatrix}, \qquad B = \begin{bmatrix} 4 & 1 \\ -1 & 2 \end{bmatrix},

the sum is

A+B=[5−1−15].A + B = \begin{bmatrix} 5 & -1 \\ -1 & 5 \end{bmatrix}.

With x=(1,2)\mathbf{x} = (1, 2) the separate outputs are Ax=(−3,6)A\mathbf{x} = (-3, 6) and Bx=(6,3)B\mathbf{x} = (6, 3), which add to (3,9)(3, 9), and directly (A+B)x=(5−2,−1+10)=(3,9)(A + B)\mathbf{x} = (5 - 2, -1 + 10) = (3, 9). The 3×23 \times 2 matrix GG cannot be added to AA, because GG has a third row with nothing to match.

Scalar multiple of a matrix

Definition For a scalar cc and an m×nm \times n matrix A=[aij]A = [a_{ij}], the scalar multiple cAcA is the m×nm \times n matrix with entries

(cA)ij=c aij.(cA)_{ij} = c\,a_{ij}.

The matrix (−1)A(-1)A is written −A-A, and for matrices of the same size A−BA - B means A+(−1)BA + (-1)B. For every x\mathbf{x} in Rn\mathbb{R}^n the scalar multiple satisfies (cA)x=c(Ax)(cA)\mathbf{x} = c(A\mathbf{x}).

In plain words Multiply every entry by the same number. Every output is then multiplied by that number too.

Example With AA, BB and x=(1,2)\mathbf{x} = (1, 2) as above,

3A=[3−609],A−B=[−3−311].\begin{gathered} 3A = \begin{bmatrix} 3 & -6 \\ 0 & 9 \end{bmatrix}, \\[0.4em] A - B = \begin{bmatrix} -3 & -3 \\ 1 & 1 \end{bmatrix}. \end{gathered}

Then (3A)x=(3−12,0+18)=(−9,18)(3A)\mathbf{x} = (3 - 12, 0 + 18) = (-9, 18), which is 3(−3,6)=3(Ax)3(-3, 6) = 3(A\mathbf{x}). Likewise (A−B)x=(−3−6,1+2)=(−9,3)(A - B)\mathbf{x} = (-3 - 6, 1 + 2) = (-9, 3), which equals Ax−Bx=(−3,6)−(6,3)A\mathbf{x} - B\mathbf{x} = (-3, 6) - (6, 3).

Transpose

Definition The transpose of an m×nm \times n matrix A=[aij]A = [a_{ij}] is the n×mn \times m matrix A⊤A^{\top} with entries

(A⊤)ij=aji(A^{\top})_{ij} = a_{ji}

for i=1,…,ni = 1, \dots, n and j=1,…,mj = 1, \dots, m. Row ii of AA becomes column ii of A⊤A^{\top}, so the entry in row ii and column jj of AA moves to row jj and column ii without changing its value. Transposing twice returns the original matrix, (A⊤)⊤=A(A^{\top})^{\top} = A. The superscript ⊤\top names an operation, not a power. The transpose only rearranges entries, and in general it does not undo multiplication by AA.

In plain words Turn each row into a column. The two numbers in the size swap places, and in a square matrix the entries on the diagonal from the top left stay where they are.

Example For the gift shop matrix,

G=[231102],G⊤=[210312],G = \begin{bmatrix} 2 & 3 \\ 1 & 1 \\ 0 & 2 \end{bmatrix}, \qquad G^{\top} = \begin{bmatrix} 2 & 1 & 0 \\ 3 & 1 & 2 \end{bmatrix},

so the 3×23 \times 2 matrix has a 2×32 \times 3 transpose. For the square matrix MM from above,

M=[3−124],M⊤=[32−14],\begin{gathered} M = \begin{bmatrix} 3 & -1 \\ 2 & 4 \end{bmatrix}, \\[0.4em] M^{\top} = \begin{bmatrix} 3 & 2 \\ -1 & 4 \end{bmatrix}, \end{gathered}

and only the two entries off the diagonal trade places. Since G⊤G^{\top} accepts vectors in R3\mathbb{R}^3, it can be applied to an output of GG, but it does not undo GG. With x=(1,0)\mathbf{x} = (1, 0) we get Gx=(2,1,0)G\mathbf{x} = (2, 1, 0), and then G⊤(Gx)=(5,7)G^{\top}(G\mathbf{x}) = (5, 7), not x\mathbf{x}.

Check yourself

  1. Let
K=[1−2031−1].K = \begin{bmatrix} 1 & -2 & 0 \\ 3 & 1 & -1 \end{bmatrix}.

Give the size of KK, the entry k23k_{23}, and the transpose K⊤K^{\top} with its size.

Show the answer

The matrix KK has 2 rows and 3 columns, so its size is 2×32 \times 3. The entry in row 2 and column 3 is k23=−1k_{23} = -1. Turning rows into columns gives

K⊤=[13−210−1],K^{\top} = \begin{bmatrix} 1 & 3 \\ -2 & 1 \\ 0 & -1 \end{bmatrix},

which is 3×23 \times 2.

  1. With KK as above and z=(2,1,4)\mathbf{z} = (2, 1, 4), compute KzK\mathbf{z} row by row, then check the result with the columns.
Show the answer

The product is defined, since KK has three columns and z\mathbf{z} has three entries. By rows, 1⋅2+(−2)⋅1+0⋅4=01 \cdot 2 + (-2) \cdot 1 + 0 \cdot 4 = 0 and 3⋅2+1⋅1+(−1)⋅4=33 \cdot 2 + 1 \cdot 1 + (-1) \cdot 4 = 3, so Kz=(0,3)K\mathbf{z} = (0, 3). By columns,

Kz=2[13]+1[−21]+4[0−1]=[26]+[−21]+[0−4]=[03].\begin{aligned} K\mathbf{z} &= 2\begin{bmatrix} 1 \\ 3 \end{bmatrix} + 1\begin{bmatrix} -2 \\ 1 \end{bmatrix} + 4\begin{bmatrix} 0 \\ -1 \end{bmatrix} \\[0.4em] &= \begin{bmatrix} 2 \\ 6 \end{bmatrix} + \begin{bmatrix} -2 \\ 1 \end{bmatrix} + \begin{bmatrix} 0 \\ -4 \end{bmatrix} = \begin{bmatrix} 0 \\ 3 \end{bmatrix}. \end{aligned}
  1. Let y=(1,2)\mathbf{y} = (1, 2). Which of KyK\mathbf{y}, K+K⊤K + K^{\top} and K⊤yK^{\top}\mathbf{y} are defined? Compute those that are.
Show the answer

The product KyK\mathbf{y} is not defined. The matrix KK has three columns, so it needs an input with three entries, and y\mathbf{y} has two. The sum K+K⊤K + K^{\top} is not defined either, because KK is 2×32 \times 3 and K⊤K^{\top} is 3×23 \times 2, and only matrices of the same size can be added. The product K⊤yK^{\top}\mathbf{y} is defined, since K⊤K^{\top} has two columns, and its output has three entries,

K⊤y=1[1−20]+2[31−1]=[70−2].K^{\top}\mathbf{y} = 1\begin{bmatrix} 1 \\ -2 \\ 0 \end{bmatrix} + 2\begin{bmatrix} 3 \\ 1 \\ -1 \end{bmatrix} = \begin{bmatrix} 7 \\ 0 \\ -2 \end{bmatrix}.

How this lesson connects