1.4 - Calculating with matrices

1.4 - Calculating with matrices

A matrix keeps related numbers in a fixed arrangement, and that arrangement controls which calculations are possible. Learn to recognise matrix dimensions, combine entries and calculate products, including non-square matrices with at most three rows and columns.

Dimensions and special matrices

How can one calculation turn three quantities into two totals? A matrix can record the weights for each total, but first we need a precise way to describe its shape.

For example,

A=(2−13041)\mathbf A=\begin{pmatrix}2&-1&3\\0&4&1\end{pmatrix}

has two horizontal rows and three vertical columns. Its entry in row 2, column 3 is 11. The order of the numbers matters: moving an entry changes the matrix.

Dimensions

An m×nm\times n matrix is a rectangular array with mm rows and nn columns. Its dimensions, or order, are rows first, columns second. A matrix is square when m=nm=n. Two matrices are equal exactly when they have the same dimensions and equal corresponding entries.

Thus A\mathbf A is 2×32\times3, while (2−1)\begin{pmatrix}2&-1\end{pmatrix} is 1×21\times2 and (2−1)\begin{pmatrix}2\\-1\end{pmatrix} is 2×12\times1. A row and a column containing the same numbers are different matrices.

Zero and identity matrices

A zero matrix, written O\mathbf O, has every entry zero; it may be rectangular. An identity matrix, or unit matrix, is square, with ones on its main diagonal from top left to bottom right, and zeros elsewhere. For example,

O2×3=(000000),I3=(100010001).\mathbf O_{2\times3}=\begin{pmatrix}0&0&0\\0&0&0\end{pmatrix},\qquad \mathbf I_3=\begin{pmatrix}1&0&0\\0&1&0\\0&0&1\end{pmatrix}.

The subscripts identify the size when it is useful. We often write simply I\mathbf I or O\mathbf O when the dimensions are clear. An all-ones matrix is not an identity matrix: its off-diagonal entries would mix quantities together. Multiplication below explains why the identity leaves quantities unchanged.

Key idea

Read the dimensions before calculating. The shape is part of a matrix's meaning, not just its layout.

Distinguish a zero matrix from an identity matrix in this check.

Addition and scalar multiplication

Matrices with the same dimensions can be combined entry by entry, because each position then has a matching position in the other matrix.

For instance, the top-left entry of A+B\mathbf A+\mathbf B is the sum of the two top-left entries. You cannot add a 2×32\times3 matrix to a 3×23\times2 matrix, even though both have six entries: their positions do not match.

Scalar multiplication

A scalar is a single number. Multiplying a matrix by a scalar multiplies every entry by that number. Matrix addition and subtraction require equal dimensions and operate on corresponding entries.

Use brackets to keep a negative scalar or a subtraction attached to every entry it affects.

Worked example 1: combining rectangular matrices

Question. Given

A=(2−13041),B=(12−23−15),\mathbf A=\begin{pmatrix}2&-1&3\\0&4&1\end{pmatrix},\qquad \mathbf B=\begin{pmatrix}1&2&-2\\3&-1&5\end{pmatrix},

find 2A−B2\mathbf A-\mathbf B.

Plan. Double every entry of A\mathbf A, then subtract corresponding entries of B\mathbf B.

Both matrices are 2×32\times3, so the subtraction is defined:

2A−B=(4−26082)−(12−23−15)=(3−48−39−3).\begin{aligned} 2\mathbf A-\mathbf B &=\begin{pmatrix}4&-2&6\\0&8&2\end{pmatrix} -\begin{pmatrix}1&2&-2\\3&-1&5\end{pmatrix}\\ &=\begin{pmatrix}3&-4&8\\-3&9&-3\end{pmatrix}. \end{aligned}

In particular, the top-right entry is 6−(−2)=86-(-2)=8.

Answer. (3−48−39−3)\begin{pmatrix}3&-4&8\\-3&9&-3\end{pmatrix}.

Check. Adding B\mathbf B to the answer gives (4−26082)=2A\begin{pmatrix}4&-2&6\\0&8&2\end{pmatrix}=2\mathbf A, reversing the subtraction.

Keep the same matrices and change which one is doubled. The scalar belongs to its matrix, not to the whole expression.

Worked example 2: moving the scalar

Question. For the same A\mathbf A and B\mathbf B, find A−2B\mathbf A-2\mathbf B.

Plan. Double B\mathbf B, then subtract it entry by entry from A\mathbf A.

A−2B=(2−13041)−(24−46−210)=(0−57−66−9).\begin{aligned} \mathbf A-2\mathbf B &=\begin{pmatrix}2&-1&3\\0&4&1\end{pmatrix} -\begin{pmatrix}2&4&-4\\6&-2&10\end{pmatrix}\\ &=\begin{pmatrix}0&-5&7\\-6&6&-9\end{pmatrix}. \end{aligned}

The bottom-middle entry is 4−(−2)=64-(-2)=6, so a negative entry being subtracted increases the result.

Answer. (0−57−66−9)\begin{pmatrix}0&-5&7\\-6&6&-9\end{pmatrix}.

Check. The result plus 2B2\mathbf B recovers all six entries of A\mathbf A.

These operations preserve the dimensions. Multiplying by zero gives the zero matrix of the same shape.

Watch out

Subtracting a matrix changes the sign of every entry being subtracted. For example, 4−(−2)=64-(-2)=6, not 22.

Apply that sign rule to fresh matrices.

Matrix products

A matrix product combines a row from the left matrix with a column from the right matrix, multiplying matching entries and adding the results.

Suppose two totals use three quantities with weights (1,2,−1)(1,2,-1) and (0,3,2)(0,3,2). If the quantities are 2,−1,32,-1,3, the totals are 1(2)+2(−1)−1(3)=−31(2)+2(-1)-1(3)=-3 and 0(2)+3(−1)+2(3)=30(2)+3(-1)+2(3)=3. These are precisely the entries of a matrix-column product.

[DIAGRAM: asset_name: A row meets a column; asset_slug: cam9231-matrix-row-column; caption: A product entry uses one row from the left and one column from the right; description: Exact labelled matrices A with rows 1 2 minus 1 and 0 3 2, and B with rows 2 1, minus 1 0, 3 minus 2. The second row of A and second column of B are highlighted. Their three paired products 0 times 1, 3 times 0 and 2 times minus 2 add to minus 4, the bottom-right entry of AB. A dimension strip shows 2 by 3 times 3 by 2 gives 2 by 2.]
A product entry uses one row from the left and one column from the right

Matrix multiplication

If A\mathbf A has dimensions m×nm\times n and B\mathbf B has dimensions n×pn\times p, then AB\mathbf{AB} has dimensions m×pm\times p. Its entry in row ii, column jj is

(AB)ij=ai1b1j+⋯+ainbnj.(\mathbf{AB})_{ij}=a_{i1}b_{1j}+\cdots+a_{in}b_{nj}.

The inner dimensions must match. Not in the formula booklet: learn it.

Each row needs exactly as many entries as the chosen column. Multiplying corresponding positions without adding is a different operation, not the matrix product.

Worked example 3: one column of output

Question. Calculate

(12−1032)(2−13).\begin{pmatrix}1&2&-1\\0&3&2\end{pmatrix} \begin{pmatrix}2\\-1\\3\end{pmatrix}.

Plan. Check 2×32\times3 times 3×13\times1, then take each row against the column.

(1(2)+2(−1)+(−1)(3)0(2)+3(−1)+2(3))=(−33).\begin{pmatrix}1(2)+2(-1)+(-1)(3)\\0(2)+3(-1)+2(3)\end{pmatrix} =\begin{pmatrix}-3\\3\end{pmatrix}.

The result has two rows and one column, as predicted.

Answer. (−33)\begin{pmatrix}-3\\3\end{pmatrix}.

Check. Build the same output as a weighted sum of the left matrix's columns:

2(10)−(23)+3(−12)=(−33).2\begin{pmatrix}1\\0\end{pmatrix} -\begin{pmatrix}2\\3\end{pmatrix} +3\begin{pmatrix}-1\\2\end{pmatrix} =\begin{pmatrix}-3\\3\end{pmatrix}.

Now append a second column to the right matrix. The original output remains its first column; only the new column needs new calculations.

Worked example 4: two columns of output

Question. Find AB\mathbf{AB}, where

A=(12−1032),B=(21−103−2).\mathbf A=\begin{pmatrix}1&2&-1\\0&3&2\end{pmatrix},\qquad \mathbf B=\begin{pmatrix}2&1\\-1&0\\3&-2\end{pmatrix}.

Plan. Use each of the two rows against each of the two columns.

AB=(2−2−31+0+20−3+60+0−4)=(−333−4).\mathbf{AB} =\begin{pmatrix} 2-2-3&1+0+2\\ 0-3+6&0+0-4 \end{pmatrix} =\begin{pmatrix}-3&3\\3&-4\end{pmatrix}.

For example, row 2 with column 2 gives 0(1)+3(0)+2(−2)=−40(1)+3(0)+2(-2)=-4.

Answer. AB=(−333−4)\mathbf{AB}=\begin{pmatrix}-3&3\\3&-4\end{pmatrix}.

Check. For the new output column, the weighted-column calculation is
(10)−2(−12)=(3−4)\begin{pmatrix}1\\0\end{pmatrix}-2\begin{pmatrix}-1\\2\end{pmatrix}=\begin{pmatrix}3\\-4\end{pmatrix}; the first column was checked above.

This answers the opening question: two rows of weights turn three inputs into two totals. For this rectangular pair, BA\mathbf{BA} is also defined, but it is 3×33\times3, so it cannot equal AB\mathbf{AB}. With a 2×32\times3 matrix and a 3×13\times1 column, the reverse product is not defined at all.

Watch out

For AB\mathbf{AB}, use rows of A\mathbf A and columns of B\mathbf B. Check compatibility afresh for BA\mathbf{BA}; neither its existence nor its value follows from AB\mathbf{AB}.

Calculate a compatible rectangular product without changing its order.

Quick Recap

  • Dimensions mean rows then columns; zero matrices may be rectangular, identities are square.
  • Add or subtract only matrices with equal dimensions; a scalar multiplies every entry.
  • In a product, inner dimensions match and outer dimensions give the result's shape.
  • Each output entry is one row-column scalar product.

Two rows of weights can turn three inputs into two totals: a 2×32\times3 matrix multiplies a 3×13\times1 column to give a 2×12\times1 column.