Matrix multiplication
Before reading this guide, it is strongly recommended that you read Guide: Introduction to matrices.
| Narration of study guide: |
What is matrix multiplication?
A matrix is a rectangular array or table, with entries in rows and columns. Understanding matrices can make solving systems of linear equations more efficient and opens the door to doing mathematics in many dimensions.
A \(m \times n\) matrix is a rectangular array of \(mn\) entries set out in \(m\) rows and \(n\) columns. You can write it like so:
\[ \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \]
This matrix has dimension \(m \times n\).
The entries in a matrix are usually numbers, but they can be other mathematical objects. Any type of number may be an entry in your matrix, positive or negative, rational or irrational, real or complex. Note here that while entries can be other mathematical objects, for this study guide you will use entries within the real numbers.
Matrices are a fundamental tool within linear algebra, and they have a wide range of real-life applications. They are used in computer graphics, data analysis, search engine optimization, cryptography, economics, robotics, genetics, quantum mechanics, and many more places. Matrices are used anywhere where information in multiple variables needs to be analyzed and calculated efficiently.
A key skill in matrix arithmetic is matrix multiplication. This is different from the scalar multiplication that you saw in Guide: Introduction to matrices, in that instead of multiplying a matrix by a constant, you are multiplying a matrix by another matrix. In this guide, you will see how and when you can multiply together two matrices, as well as how matrix multiplication interacts with other arithmetic operations as seen in Guide: Introduction to matrices.
Initial example: \(2\times 2\) matrices
First, let’s see how you can multiply two \(2 \times 2\) matrices via an example. Let \(A\) and \(B\) be \(2 \times 2\) matrices with entries given below:
\[A = \begin{bmatrix} 1 & -1 \\ 2 & 1 \end{bmatrix} \quad B =\begin{bmatrix} 3 & 0 \\ 1 & -1 \end{bmatrix}\]
How can you find their product \(AB\)? Since a matrix is defined by its entries, you could think about working out each entry. The idea for the \((i,j)\)th entry is to find a ‘product’ of the \(i\)th row of \(A\) by the \(j\)th column of \(B\).
For the \((1,1)\)th entry of \(AB\), you can match the \(1\)st row of \(A\) to the \(1\)st column of \(B\), multiply the matched entries together, and add up all the entries. The first row of \(A\) is \(\begin{matrix}1 & -1\end{matrix}\), and the first column of \(B\) is \(\begin{matrix}3 & 1\end{matrix}\). Here, \(1\) (from \(A\)) and \(3\) (from \(B\)) are matched, and \(-1\) (from \(A\)) and \(1\) (from \(B\)) are matched. Multiplying these together and adding gives \[[AB]_{1,1} = 1\cdot 3 + (-1)\cdot 1 = 3 - 1 = 2\]
This process is illustrated in the following equation, with the appropriate row and column highlighted in bold:
\[AB = \begin{bmatrix} \mathbf{1} & \mathbf{-1} \\ 2 & 1 \end{bmatrix}\begin{bmatrix} \mathbf{3} & 0 \\ \mathbf{1} & -1 \end{bmatrix} = \begin{bmatrix}1\cdot 3 + (-1)\cdot 1 & \\ & \end{bmatrix} = \begin{bmatrix}2 & \\ & \end{bmatrix}\]
You can now repeat this process to find the other entries. For the \((1,2)\)th entry of \(AB\), you can match the \(1\)st row of \(A\) to the \(2\)nd column of \(B\), multiply the matched entries together, and add up all the entries. Doing this in the matrix with the appropriate values highlighted in bold gives
\[AB = \begin{bmatrix} \mathbf{1} & \mathbf{-1} \\ 2 & 1 \end{bmatrix}\begin{bmatrix} 3 & \mathbf{0} \\ 1 & \mathbf{-1} \end{bmatrix} = \begin{bmatrix}2 & 1\cdot 0 + (-1)\cdot (-1)\\ & \end{bmatrix} = \begin{bmatrix}2 & 1\\ & \end{bmatrix}\]
For the \((2,1)\)th entry of \(AB\), you can match the \(2\)nd row of \(A\) to the \(1\)st column of \(B\). Doing this in the matrix with the appropriate values highlighted in bold gives
\[AB = \begin{bmatrix} 1 & -1 \\ \mathbf{2} & \mathbf{1} \end{bmatrix}\begin{bmatrix} \mathbf{3} & 0 \\ \mathbf{1} & -1 \end{bmatrix} = \begin{bmatrix}2 & 1\\ 2\cdot 3 + 1\cdot 1& \end{bmatrix} = \begin{bmatrix}2 & 1\\ 7 & \end{bmatrix}\]
Finally, for the \((2,2)\)th entry of \(AB\), you can match the \(2\)nd row of \(A\) to the \(2\)nd column of \(B\). Doing this in the matrix with the appropriate values highlighted in bold gives
\[AB = \begin{bmatrix} 1 & -1 \\ \mathbf{2} & \mathbf{1} \end{bmatrix}\begin{bmatrix} 3 & \mathbf{0} \\ 1 & \mathbf{-1} \end{bmatrix} = \begin{bmatrix}2 & 1\\ 7 & 2\cdot 0 + 1\cdot (-1)\end{bmatrix} = \begin{bmatrix}2 & 1\\ 7 & -1\end{bmatrix}\]
and this is your final answer!
This process can be generalised to any two \(2\times 2\) matrices.
Suppose that \[A = \begin{bmatrix} a & b \\ c & d \end{bmatrix} \quad\textsf{ and }\quad B =\begin{bmatrix} e & f \\ g & h \end{bmatrix}\] are two \(2\times 2\) matrices. Then their product \(AB\) is the matrix \[AB = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\begin{bmatrix} e & f \\ g & h \end{bmatrix} = \begin{bmatrix}ae + bg & af + bh \\ ce + dg & cf + dh \end{bmatrix}\]
Going ahead, it’s more important to learn the process of how this formula is found, rather than learn the formula itself. This is because this process can be extended to work out general matrix multiplication, and not every matrix is a \(2\times 2\) matrix!
The ‘product’ of the \(i\)th row of \(A\) and the \(j\)th column of \(B\) to form the \((i,j)\)th entry of \(AB\) can be recognised as the scalar product of the vectors given by \(i\)th row of \(A\) and the \(j\)th column of \(B\). For more on the scalar product, please see Guide: The scalar product.
Let \(A\) and \(B\) be the following two \(2\times 2\) matrices. \[A =\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \quad\textsf{ and }\quad B = \begin{bmatrix} 0 & 5 \\ 6 & 7 \end{bmatrix}\]
You can follow the process illustrated above to write
\[AB = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\begin{bmatrix} 0 & 5 \\ 6 & 7 \end{bmatrix} = \begin{bmatrix} 1 \cdot 0 + 2 \cdot 6 & 1 \cdot 5 + 2 \cdot 7 \\ 3 \cdot 0 + 4 \cdot 6 & 3 \cdot 5 + 4 \cdot 7 \end{bmatrix}\]
By carrying out these calculations you will have, \[AB=\begin{bmatrix} 0 + 12 & 5 + 14 \\ 0 + 24 & 15 + 28 \end{bmatrix}=\begin{bmatrix} 12 & 19 \\ 24 & 43 \end{bmatrix}\]
Unlike for numbers, the order of multiplication matters in matrix multiplication. This is because you take the rows from the left matrix in the product, and columns from the right matrix; these could be different if the order swaps between the left and right matrices! You can see this in the following example.
Using the \(A\) and \(B\) from Example 1, you can compute \(BA\):
\[BA = \begin{bmatrix} 0 & 5 \\ 6 & 7 \end{bmatrix}\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} = \begin{bmatrix} 0 \cdot 1 + 5 \cdot 3 & 0 \cdot 2 + 5 \cdot 4 \\ 6 \cdot 1 + 7 \cdot 3 & 6 \cdot 2 + 7 \cdot 4 \end{bmatrix}\] By carrying out these calculations you will have, \[BA=\begin{bmatrix} 0 + 15 & 0 + 20 \\ 6 + 21 & 12 + 28 \end{bmatrix}=BA=\begin{bmatrix} 15 & 20 \\ 27 & 40 \end{bmatrix}\]
You can compare \(AB\) and \(BA\).
\[AB =\begin{bmatrix} 12 & 19 \\ 24 & 43 \end{bmatrix}, \quad BA = \begin{bmatrix} 15 & 20 \\ 27 & 40 \end{bmatrix}\] and here, none of the entries match! Since \(AB \neq BA\), this example confirms that matrix multiplication is not commutative in general.
Generalizing to all matrices
Not all matrices are \(2\times 2\) matrices. How would you multiply two \(3\times 3\) matrices? Or two \(40\times 40\) matrices?
The idea in multiplying \(2\times 2\) matrices was the following:
The idea for the \((i,j)\)th entry is to find a ‘product’ of the \(i\)th row of \(A\) by the \(j\)th column of \(B\). This is done by matching entries in the \(i\)th row of \(A\) to entries in the \(j\)th column of \(B\), multiplying the matched entries, and adding them all together.
This applies to matrices \(A\) and \(B\) of any dimensions, provided that there is a match for all the entries in the \(i\)th row of \(A\) to the \(j\)th column of \(B\). What this means is that two matrices \(A\) and \(B\) can be multiplied together if and only if the number of columns of \(A\) equals the number of rows of \(B\). This is because the number of entries in the \(i\)th row of \(A\) is the same as the number of columns of \(A\), and the number of entries in the \(j\)th column of \(B\) is the same as the number of rows of \(B\).
Here is a definition of matrix multiplication for more general matrices.
Let \(A\) be an \(m \times n\) matrix and \(B\) be an \(n \times p\) matrix:
\[A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \quad B = \begin{bmatrix} b_{11} & b_{12} & \cdots & b_{1p} \\ b_{21} & b_{22} & \cdots & b_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ b_{n1} & b_{n2} & \cdots & b_{np} \end{bmatrix}\]
The product \(C = AB\) is an \(m \times p\) matrix, where each entry \(c_{ij}\) of \(C\) is given by the following summation formula:
\[ \begin{aligned} c_{ij} &= a_{i1}b_{1j} + a_{i2}b_{2j} + \ldots + a_{in}b_{nj}\\[0.5em] &= \sum_{k=1}^{n} a_{ik} b_{kj} \end{aligned} \] In the context of vectors, this is the scalar product of the \(i\)th row of \(A\) with the \(j\)th column of \(B\). You can read more about this at Guide: The scalar product.
For more about the sigma notation in this definition, you can read more about it in Guide: Introduction to sigma notation. It’s important to remember:
You can only multiply matrices \(A\) and \(B\) if \(A\) has the same number of columns as \(B\) has rows, otherwise this operation is undefined.
As seen in Example 2, matrix multiplication is non-commutative. This means that it is not always true for matrices that \(AB = BA\). In fact, in some cases, \(AB\) may be defined when \(BA\) is not defined.
To take a better look at this unusual property, you can now see some examples of this in action. You can use the interactive figure below to investigate multiplying a \(2\times 3\) matrix by a \(3\times 2\) matrix (and the other way around). You are able to change the numbers inside the matrices \(A,B,C,D\) by clicking on the entry you want to change.
| 58 | 64 |
| 139 | 154 |
| 39 | 54 | 69 |
| 49 | 68 | 87 |
| 59 | 82 | 105 |
Let \(A\) be a \(2 \times 3\) matrix and \(B\) be a \(3 \times 2\) matrix.
\[A = \begin{bmatrix} -2 & 1 & 3 \\ 4 & -5 & 2 \end{bmatrix} \quad\textsf{ and }\quad B =\begin{bmatrix} 3 & -1 \\ -2 & 5 \\ 1 & -2 \end{bmatrix}\]
You can work out the product \(AB\). Since \(A\) is \(2 \times 3\) and \(B\) is \(3 \times 2\), the product \(AB\) is a \(2 \times 2\) matrix. You can work out each entry of \(AB\) by following the row and column matching as described above.
\[ \begin{aligned} AB &= \begin{bmatrix} \mathbf{-2} & \mathbf{1} & \mathbf{3} \\ 4 & -5 & 2 \end{bmatrix}\begin{bmatrix} \mathbf{3} & -1 \\ \mathbf{-2} & 5 \\ \mathbf{1} & -2 \end{bmatrix}\\[0.5em] &= \begin{bmatrix}(-2)\cdot 3 + 1\cdot(-2) + 3\cdot 1 & \\ & \end{bmatrix} = \begin{bmatrix}-5 & \\ & \end{bmatrix} \end{aligned} \]
For the \((1,2)\)th entry of \(AB\), use the first row of \(A\) and the second column of \(B\).
\[ \begin{aligned} AB &= \begin{bmatrix} \mathbf{-2} & \mathbf{1} & \mathbf{3} \\ 4 & -5 & 2 \end{bmatrix}\begin{bmatrix} 3 & \mathbf{-1} \\ -2 & \mathbf{5} \\ 1 & \mathbf{-2} \end{bmatrix}\\[0.5em] &= \begin{bmatrix}-5 & (-2)\cdot (-1) + 1\cdot 5 + 3\cdot (-2) \\ & \end{bmatrix} = \begin{bmatrix}-5 & 1\\ & \end{bmatrix} \end{aligned} \]
For the \((2,1)\)th entry of \(AB\), you can match the \(2\)nd row of \(A\) to the \(1\)st column of \(B\).
\[ \begin{aligned} AB &= \begin{bmatrix} -2 & 1 & 3 \\ \mathbf{4} & \mathbf{-5} & \mathbf{2} \end{bmatrix}\begin{bmatrix} \mathbf{3} & -1 \\ \mathbf{-2} & 5 \\ \mathbf{1} & -2 \end{bmatrix}\\[0.5em] &= \begin{bmatrix}-5 & 1\\ 4\cdot 3 + (-5)\cdot (-2) + 2\cdot 1 & \end{bmatrix} = \begin{bmatrix}-5 & 1\\ 24 & \end{bmatrix} \end{aligned} \]
Finally, for the \((2,2)\)th entry of \(AB\), you can match the \(2\)nd row of \(A\) to the \(2\)nd column of \(B\).
\[ \begin{aligned} AB &= \begin{bmatrix} -2 & 1 & 3 \\ \mathbf{4} & \mathbf{-5} & \mathbf{2} \end{bmatrix}\begin{bmatrix} 3 & \mathbf{-1} \\ -2 & \mathbf{5} \\ 1 & \mathbf{-2} \end{bmatrix}\\[0.5em] &= \begin{bmatrix}-5 & 1\\ 24 & 4\cdot (-1) + (-5)\cdot 5 + 2\cdot (-2) \end{bmatrix} = \begin{bmatrix}-5 & 1\\ 24 & -33\end{bmatrix} \end{aligned} \]
and this is your final answer! You don’t have to write all of this out, you could instead write
\[AB = \begin{bmatrix} (-2) \cdot 3 + 1 \cdot (-2) + 3 \cdot 1 & (-2) \cdot (-1) + 1 \cdot 5 + 3 \cdot (-2) \\ 4 \cdot 3 + (-5) \cdot (-2) + 2 \cdot 1 & 4 \cdot (-1) + (-5) \cdot 5 + 2 \cdot (-2) \end{bmatrix} =\begin{bmatrix} -5 & 1\\ 24 & -33 \end{bmatrix}\]
Now you can work out \(BA\). Since \(B\) is \(3 \times 2\) and \(A\) is \(2 \times 3\), the product \(BA\) is a \(3 \times 3\) matrix:
\[BA = \begin{bmatrix} 3 \cdot (-2) + (-1) \cdot 4 & 3 \cdot 1 + (-1) \cdot (-5) & 3 \cdot 3 + (-1) \cdot 2 \\ (-2) \cdot (-2) + 5 \cdot 4 & (-2) \cdot 1 + 5 \cdot (-5) & (-2) \cdot 3 + 5 \cdot 2 \\ 1 \cdot (-2) + (-2) \cdot 4 & 1 \cdot 1 + (-2) \cdot (-5) & 1 \cdot 3 + (-2) \cdot 2 \end{bmatrix}\] By carrying out these multiplications and adding you will have, \[BA=\begin{bmatrix} -6 - 4 & 3 + 5 & 9 - 2 \\ 4 + 20 & -2 - 25 & -6 + 10 \\ -2 - 8 & 1 + 10 & 3 - 4 \end{bmatrix} =\begin{bmatrix} -10 & 8 & 7 \\ 24 & -27 & 4 \\ -10 & 11 & -1 \end{bmatrix}\]
Here you can see that not only are the entries in \(AB\) not equal to those in \(BA\), but the matrix \(AB\) is of different dimension to \(BA\). They are definitely not equal!
Here’s an example where one product of matrices \(AB\) is defined, but where the product \(BA\) isn’t defined:
Now, you can try multiplying a \(2 \times 3\) matrix and a \(3 \times 1\) matrix.
\[A =\begin{bmatrix} 0 & 2 & -1\\ 4 & 0 & 3 \\ \end{bmatrix}\quad\textsf{and}\quad B =\begin{bmatrix} 1\\ -2\\ 0 \end{bmatrix}\]
You can compute \(AB\) since, in order to multiply matrices, you require the first matrix to have the same number of columns as our second matrix has rows. Here \(A\) has 3 columns, and \(B\) has 3 rows. Let’s calculate \(AB\),
\[AB = \begin{bmatrix} 0 \cdot 1 + 2 \cdot (-2) + (-1) \cdot 0 \\ 4 \cdot 1 + 0 \cdot (-2) + 3 \cdot 0 \end{bmatrix}\] Simplifying gives
\[AB = \begin{bmatrix} 0 + -4 + 0 \\ 4 + 0 + 0 \end{bmatrix} = \begin{bmatrix} -4 \\ 4 \end{bmatrix}\]
So here \(AB\) is the \(2 \times 1\) matrix given above.
However, in this example, \(BA\) is undefined. This is because you require the first matrix to have the same number of columns as the second matrix has rows, \(B\) here only has 1 column, whereas \(A\) has 2 rows.
Properties of matrix arithmetic
Now you’ve seen matrix addition and subtraction, scalar multiplication, and matrix multiplication, you can develop some key properties that hold through arithmetic with matrices. These properties rely on arithmetic properties of the set of numbers that give the entries.
For any three matrices \(A,B,C\) and any two scalars \(\alpha,\beta\), the following properties hold where the operations make sense:
Matrix addition is commutative; that is \(A+B=B+A\).
Matrix addition is associative; that is \((A+B)+C=A+(B+C)\).
Scalar multiplication is distributive across matrix addition; that is, \(\alpha(A + B) = \alpha A + \alpha B\).
Scalar multiplication is distributive across scalar addition; that is, \((\alpha+\beta)A = \alpha A + \beta A\).
Matrix multiplication is associative, that is \((AB)C=A(BC)\).
Matrix multiplication is distributive across matrix addition; that is \(A(B+C)=AB+AC\) and \((A+B)C=AC+BC\).
Scalar multiplication can occur at any stage of matrix multiplication; that is \(\alpha(AB) = (\alpha A)B = A(\alpha B)\).
You can see proofs of these properties in [Proof sheet: Properties of matrix arithmetic]. Here’s an example of these properties in action:
Let’s calculate \((A + 2B)C\), for \[A =\begin{bmatrix} -2 & 3 \\ 0 & 1 \\ \end{bmatrix} \qquad B =\begin{bmatrix} -1 & 2 \\ -1 & -3 \\ \end{bmatrix}\qquad C = \begin{bmatrix}1&1\\0&1\end{bmatrix}\]
There are two ways of going about doing this. You could expand the brackets using property (g) above to get \[(A + 2B)C = AC + 2BC\] and work out \(AC + 2BC\) instead. However, working out \((A + 2B)C\) involves doing one lot of matrix multiplication, and working out \(AC + 2BC\) involves two lots of matrix multiplication. Typically, to avoid mistakes you want to reduce the amount of computation you have to do; since matrix multiplication is computationally heavy, doing one set is preferable to doing two!
So you can work out \((A + 2B)C\). In Example 8 of Guide: Introduction to matrices you worked out that \[A + 2B = \begin{bmatrix} -3 & 6 \\ -3 & -9 \\ \end{bmatrix}\]
Now you can do the matrix multiplication to get:
\[ \begin{aligned} (A + 2B)C &= \begin{bmatrix} -3 & 6 \\ -3 & -9 \\ \end{bmatrix}\begin{bmatrix} 1 & 1 \\ 0 & 1 \\ \end{bmatrix}\\[0.5em] &= \begin{bmatrix} (-3) \cdot 1 + 6 \cdot 0 & (-3) \cdot 1 + 6 \cdot 1 \\ (-3) \cdot 1 + (-9) \cdot 0 & (-3) \cdot 1 + (-9) \cdot 1 \end{bmatrix} = \begin{bmatrix} -3 & 3 \\ -3 & -12 \end{bmatrix} \end{aligned} \] and this is your final answer. The moral of the story is not to expand brackets unless you really have to!
Quick check problems
- Decide whether the following statements are true or false.
Both \(AB\) and \(BA\) are defined for any two matrices \(A\) and \(B\).
If \(A\) is a \(p\times p\) matrix and \(B\) is a \(q\times p\) matrix, then \(BA\) is a \(p\times q\) matrix.
If \(A,B,C\) are three matrices, is it always true that \(A(B+C) = BA + CA\)? Answer: .
- When you multiply together the two matrices \[B = \begin{bmatrix} -2 & 2 \\ -2 & 2 \end{bmatrix} \quad\textsf{ and }\quad C = \begin{bmatrix} -4 & 3\\ 3 & -4\end{bmatrix}\] you get a \(2\times 2\) matrix where each column has the same entry throughout. Find the two numbers in the two columns.
Answer: The number in the left column is and the number in the right column is .
- You are given the following equation \[\begin{bmatrix} 1 & 0 & 2 \\ -1 & 0 & 1 \\ -2 & 1 & 0 \end{bmatrix} \begin{bmatrix} 0 & 1 & 2 \\ 1 & -2 & 1 \\ -1 & 2 & 0\end{bmatrix} = \begin{bmatrix} -2 & a & 2 \\ b & 1 & c \\ 1 & d & -3 \end{bmatrix}\] Find \(a,b,c,d\).
Answer: \(a =\) , \(b =\) , \(c =\) , \(d =\) .
Further reading
For more questions on this topic, please go to Questions: Matrix multiplication.
For more about how special matrices behave with matrix multiplication, please go to Guide: Matrix multiplication with special matrices.
Version history
v1.0: initial version created 04/25 by Jessica Taberner as part of a University of St Andrews VIP project.