Skip to content
RMIT University Library - Learning Lab

M3 Matrix multiplication

 

Matrices may be added and subtracted if they have the same shape. That is, the number of rows and columns is the same. Matrices may also be multiplied. However the requirements for multiplication are very different to that for addition/subtraction. This module looks at matrix multiplication.

Matrix Shape or Order of a Matrix

The shape of a matrix is the number of rows and columns. When describing a matrix you give the number of rows by the number of columns. This is sometimes called the order of the matrix. For example, the matrix \[\begin{align*} A & =\left[\begin{array}{cccc} 1 & 3 & 5 & 0\end{array}\right] \end{align*}\] is a said to be a one by four matrix as it has \(1\) row and \(4\) columns. We could also say the order of \(A\) is \(1\times4\). The matrix \[\begin{align*} B & =\left[\begin{array}{cc} 1 & 2\\ 3 & 5\\ 2 & 6\\ 4 & 7\\ 5 & 89 \end{array}\right] \end{align*}\] is a \(5\times2\) matrix because it has \(5\) rows and \(2\) columns. It has order \(5\times2\).

Conditions for Matrix Multiplication

Two matrices can be multiplied if the number of columns of the first matrix is the same as the number of rows of the second matrix. The result of the multiplication is another matrix with the same number of rows as the first matrix and the same number of columns as the second matrix. If this is not the case, the multiplication is undefined.

Symbolically let \(A\) be an \(m\times p\) matrix and let \(B\) be an \(q\times n\) matrix. Then the product \(A\times B=AB\) will be an \(m\times n\) matrix provided that \(p=q\). If \(p\neq q\) the matrix multiplication is not defined. For example, a \(2\times3\) matrix cannot be multiplied by a \(1\times4\) matrix because \(3\neq1\) whereas it is possible to multiply a \(2\times3\) matrix by a \(3\times2\) and the result will be a \(2\times2\) matrix.

Matrix Multiplication

Let \[\begin{align*} A & =\left[\begin{array}{ccc} 2 & 4 & 5\\ 7 & 8 & 9 \end{array}\right], & B & =\left[\begin{array}{cc} 1 & 6\\ 3 & 1\\ 4 & 2 \end{array}\right] \end{align*}\] we want to find, if it exists, the product \(AB\). First note that we are multiplying a \(2\times3\) matrix by a \(3\times2\) and so matrix multiplication is possible and the result will be a \(2\times2\) matrix. You should always check that the multiplication is possible.

Now the multiplication procedure is a bit strange at first. The first element of row \(1\) of the resulting matrix \(AB\) is found by multiplying each element of the first row of \(A\) by the corresponding element in the first column of \(B\) and then adding products as shown below: \[\begin{align*} AB & =\left[\begin{array}{ccc} 2 & 4 & 5\\ 7 & 8 & 9 \end{array}\right]\left[\begin{array}{cc} 1 & 6\\ 3 & 1\\ 4 & 2 \end{array}\right]\\ & =\left[\begin{array}{cc} 2\times1+4\times3+5\times4 & 2nd\;element\;of\;row\;1\\ 1st\;element\;of\;row\;2 & 2nd\;element\;of\;row\;2 \end{array}\right]\\ & =\left[\begin{array}{cc} 2+12+20 & 2nd\;element\;of\;row\;1\\ 1st\;element\;of\;row\;2 & 2nd\;element\;of\;row\;2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 2nd\;element\;of\;row\;1\\ 1st\;element\;of\;row\;2 & 2nd\;element\;of\;row\;2 \end{array}\right] \end{align*}\] The 2nd element of row \(1\) is found by multiplying each element of the first row of \(A\) by the corresponding element in the second column of \(B\) and then adding products as shown below: \[\begin{align*} AB & =\left[\begin{array}{ccc} 2 & 4 & 5\\ 7 & 8 & 9 \end{array}\right]\left[\begin{array}{cc} 1 & 6\\ 3 & 1\\ 4 & 2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 2\times6+4\times1+5\times2\\ 1st\;element\;of\;row\;2 & 2nd\;element\;of\;row\;2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 12+4+10\\ 1st\;element\;of\;row\;2 & 2nd\;element\;of\;row\;2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 26\\ 1st\;element\;of\;row\;2 & 2nd\;element\;of\;row\;2 \end{array}\right] \end{align*}\] The 1st element of row \(2\) is found by multiplying each element of the second row of \(A\) by the corresponding element in the first column of \(B\) and then adding products as shown below: \[\begin{align*} AB & =\left[\begin{array}{ccc} 2 & 4 & 5\\ 7 & 8 & 9 \end{array}\right]\left[\begin{array}{cc} 1 & 6\\ 3 & 1\\ 4 & 2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 26\\ 7\times1+8\times3+9\times4 & 2nd\;element\;of\;row\;2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 26\\ 7+24+36 & 2nd\;element\;of\;row\;2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 26\\ 67 & 2nd\;element\;of\;row\;2 \end{array}\right] \end{align*}\] Finally the the 2nd element of row \(2\) is found by multiplying each element of the second row of \(A\) by the corresponding element in the second column of \(B\) and then adding products as shown below: \[\begin{align*} AB & =\left[\begin{array}{ccc} 2 & 4 & 5\\ 7 & 8 & 9 \end{array}\right]\left[\begin{array}{cc} 1 & 6\\ 3 & 1\\ 4 & 2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 26\\ 67 & 7\times6+8\times1+9\times2 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 26\\ 7+24+36 & 42+8+18 \end{array}\right]\\ & =\left[\begin{array}{cc} 34 & 26\\ 67 & 68 \end{array}\right]. \end{align*}\]

Example \(1\):

Let \[\begin{align*} A & =\left[\begin{array}{cc} 2 & 1\\ 3 & 0 \end{array}\right], & B & =\left[\begin{array}{ccc} 2 & 1 & 0\\ 0 & 2 & 1 \end{array}\right] \end{align*}\] Find a) \(AB\;\) b) \(BA\;\) c) \(AA\;\) d) \(BB\) if they exist.

Solution:

  1. Since \(A\) is \(2\times2\) and \(B\) is \(2\times3\), the number of columns in \(A\) is equal to the number of rows in \(B\) and so \(AB\) exists and will be a \(2\times3\) matrix. We have: \[\begin{align*} AB & =\left[\begin{array}{cc} 2 & 1\\ 3 & 0 \end{array}\right]\left[\begin{array}{ccc} 2 & 1 & 0\\ 0 & 2 & 1 \end{array}\right]\\ & =\left[\begin{array}{ccc} 2\times2+1\times0 & 2\times1+1\times2 & 2\times0+1\times1\\ 3\times2+0\times0 & 3\times1+0\times2 & 3\times0+0\times1 \end{array}\right]\\ & =\left[\begin{array}{ccc} 4 & 4 & 1\\ 6 & 3 & 0 \end{array}\right]. \end{align*}\]

  2. The number of columns of \(B\) is \(3\) and the number of rows of \(A\) is \(2\). Since they are unequal, \(BA\) is not defined and \(BA\) does not exist.

  3. Since \(A\) has the same number of rows and columns1 \(A\) is called a square matrix. and so \(AA\) exists.2 We sometimes write \(AA=A^{2.}.\) We have: \[\begin{align*} AA & =\left[\begin{array}{cc} 2 & 1\\ 3 & 0 \end{array}\right]\left[\begin{array}{cc} 2 & 1\\ 3 & 0 \end{array}\right]\\ & =\left[\begin{array}{cc} 2\times2+1\times3 & 2\times1+1\times0\\ 3\times2+0\times3 & 3\times1+0\times0 \end{array}\right]\\ & =\left[\begin{array}{cc} 7 & 2\\ 6 & 3 \end{array}\right]. \end{align*}\]

  4. The number of columns and rows in \(B\) are unequal and so \(BB\) does not exist.3 Only square matrices can be squared.

Example 2:

Let \[\begin{align*} A & =\left[\begin{array}{cc} 2 & -1\\ 1 & 3 \end{array}\right], & B & =\left[\begin{array}{cc} 1 & 0\\ 2 & -1 \end{array}\right] \end{align*}\] find if they exist, a) \(AB\) and b) \(BA\).

Solution:

  1. Since the number of columns and rows is the same for matrices \(B\) and \(C\), both \(BC\) and \(CB\) exist and will be \(2\times2\) matrices. We have \[\begin{align*} AB & =\left[\begin{array}{cc} 2 & -1\\ 1 & 3 \end{array}\right]\left[\begin{array}{cc} 1 & 0\\ 2 & -1 \end{array}\right]\\ & =\left[\begin{array}{cc} 2\times1+\left(-1\right)\times2 & 2\times0+\left(-1\right)\times\left(-1\right)\\ 1\times1+3\times2 & 1\times0+3\times\left(-1\right) \end{array}\right]\\ & =\left[\begin{array}{cc} 0 & 1\\ 7 & -3 \end{array}\right] \end{align*}\] and \[\begin{align*} BA & =\left[\begin{array}{cc} 1 & 0\\ 2 & -1 \end{array}\right]\left[\begin{array}{cc} 2 & -1\\ 1 & 3 \end{array}\right]\\ & =\left[\begin{array}{cc} 1\times2+0\times1 & 1\times\left(-1\right)+0\times3\\ 2\times2+\left(-1\right)\times1 & 2\times\left(-1\right)+\left(-1\right)\times3 \end{array}\right]\\ & =\left[\begin{array}{cc} 2 & -1\\ 3 & -5 \end{array}\right] \end{align*}\]

From this example we can see that, in general, \(AB\neq BA\). We say that matrix multiplication is not commutative and the order of multiplication matters. There is an exception to this as explained in the next section.

Multiplication by the Unit Matrix

A square matrix with ones along its leading diagonal and zeros elsewhere is called a unit matrix (also called an identity matrix). The order of the unit matrix is sometimes noted with a subscript. For example,

\[\begin{align*} I_{1}=\left[1\right],\;I_{2} & =\left[\begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right],\;I_{3}=\left[\begin{array}{ccc} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{array}\right],\;I_{4}=\left[\begin{array}{cccc} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 \end{array}\right],\;I_{n}=\left[\begin{array}{ccccc} 1 & 0 & 0 & \cdots & 0\\ 0 & 1 & 0 & \cdots & 0\\ 0 & 0 & 1 & \cdots & 0\\ \vdots & \vdots & \vdots & \ddots & \vdots\\ 0 & 0 & 0 & \cdots & 1 \end{array}\right]. \end{align*}\]

The unit matrix acts like the number one in the real number system. When matrix multiplication is defined, multiplication by the unit matrix leaves the original matrix unchanged.

Multiplying a square \(n\times n\) matrix \(A\) by the unit matrix \(I_{n}\) shows that the order of multiplication does not matter.4 This is only true for square matrices That is \[\begin{align*} AI_{n} & =I_{n}A. \end{align*}\]

For example, let \(A=\left[\begin{array}{cc} 1 & 2\\ 3 & 4 \end{array}\right]\). Since \(A\) has two columns we need the unit matrix to have two rows and two columns. That is \(I=\left[\begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right]\). Then \[\begin{align*} AI & =\left[\begin{array}{cc} 1 & 2\\ 3 & 4 \end{array}\right]\left[\begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right]\\ & =\left[\begin{array}{cc} 1\times1+2\times0 & 1\times0+2\times1\\ 3\times1+3\times0 & 3\times0+4\times1 \end{array}\right]\\ & =\left[\begin{array}{cc} 1 & 2\\ 3 & 4 \end{array}\right]\\ & =A \end{align*}\] and \[\begin{align*} IA & =\left[\begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right]\left[\begin{array}{cc} 1 & 2\\ 3 & 4 \end{array}\right]\\ & =\left[\begin{array}{cc} 1\times1+0\times3 & 1\times2+0\times4\\ 0\times1+1\times3 & 0\times2+1\times4 \end{array}\right]\\ & =\left[\begin{array}{cc} 1 & 2\\ 3 & 4 \end{array}\right]\\ & =A. \end{align*}\] So, in this case, \(AI=IA=A\).

Exercises

Let \[\begin{align*} A & =\left[\begin{array}{cc} 4 & -1\\ 1 & 3 \end{array}\right],\;B=\left[\begin{array}{cc} 1 & 3\\ -2 & -4\\ 2 & 0 \end{array}\right],\;C=\left[\begin{array}{ccc} 3 & 1 & -5\\ 2 & 0 & 4\\ 1 & 6 & 3 \end{array}\right],\;D=\left[\begin{array}{c} 3\\ 2\\ -2 \end{array}\right],\;E=\left[\begin{array}{ccc} -3 & 2 & 0\\ 0 & 5 & -1 \end{array}\right]. \end{align*}\] Find (if they exist)

\[\begin{array}{lllllllcc} a)\,BA & b)\,DB & c)\,CD & d)\,AA=A^{2} & e)\,BB=B^{2} & f)\,AD & g)\,BE & h)\,EA & i)\,CB\end{array}\]

  1. \(BA=\left[\begin{array}{cc} 7 & 8\\ -12 & -10\\ 8 & -2 \end{array}\right]\)

  2. \(DB\) does not exist.

  3. \(CD=\left[\begin{array}{c} 21\\ -3\\ 9 \end{array}\right]\)

  4. \(A^{2}=\left[\begin{array}{cc} 15 & -7\\ 7 & 8 \end{array}\right]\)

  5. \(B^{2}\) does not exist.

  6. \(AD\) does not exist.

  7. \(BE=\left[\begin{array}{ccc} -3 & 17 & -3\\ 6 & -24 & 4\\ -6 & 4 & 0 \end{array}\right]\)

  8. \(EA\) does not exist.

  9. \(CB=\left[\begin{array}{cc} -9 & 5\\ 10 & 6\\ -5 & -21 \end{array}\right]\).

Download this page, M3 Matrix multiplication (PDF 128KB)

What's next... M4 Determinant of a matrix

Keywords: