Skip to main content

Matrix multiplication

A strong understanding of how to multiply matrices is critical in many fields, like computer science, robotics, statistics and economics. Just as there are rules for adding and subtracting matrices, we can multiply matrices with each other when certain requirements are met. Use this resource to learn how to multiply matrices.

Before you begin, make sure you understand the basics of matrices, including how to identify the shape or order of a matrix.

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.

Matrix multiplication results in a new matrix with the number of rows from the first matrix and the number of columns from the second matrix. This operation is only defined if the number of columns in the first matrix equals the number of rows in the second matrix.

For instance, if matrix \(A\) is \(m\times p\) and matrix \(B\) is \(q\times n\), then \(A\times B=AB\) is possible if \(p=q\). If \(p\neq q\), the product is undefined or does not exist. For example, a \(2\times3\) matrix can't be multiplied by a \(1\times4\) matrix, but can be multiplied by a \(3\times2\) matrix to produce a \(2\times2\) matrix.

The process of multiply matrices is a bit confusing. We can summarise it using the figure, but let's look at some examples.

Three 2 by 2 matrices. The first has the value a1 in row 1 column 1 and a2 in row 1 column 2. The second matrix has the value b1 in row 1 column 1 and b3 in row 2 column 1. The two matrices have a multiply symbol between them. In the third 2 by 2 matrix, which is the answer, the equation a1 times b1 plus a2 times b3 = c1 is shown in row 1 column 1.

Example 1 – multiplying matrices

Consider matrices \(A\) and \(B\).
\[A = \left[ \begin{array}{ccc} 2 & 4 & 5\\
7 & 8 & 9 \
\end{array} \right]\quad
B = \left[ \begin{array}{cc} 1 & 6\\
3 & 1\\
4 & 2
\end{array} \right] \] Find the product \(AB\), if it exists.

First, identify the orders of the matrices to determine whether multiplication is possible. \(A\) is a \(2\times3\) matrix and \(B\) is a \(3\times2\) matrix. Matrix multiplication is possible since \(A\) has \(3\) columns and \(B\) has \(3\) rows. The result will be a matrix with the same number of rows as \(A\) and columns as \(B\), so \(2\times2\).

We'll work through the multiplication one element at a time. First, we find the first element in row \(1\) of the final matrix \(AB\). To do this, we multiply each element in row \(1\) of \(A\) by the corresponding element in column \(1\) of \(B\), then add them altogether.

\[\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) & \textrm{element 2 of row 1}\\
\textrm{element 1 of row 2} & \textrm{element 2 of row 2}
\end{array} \right]\\
& = \left[ \begin{array}{cc} 2+12+20 & \textrm{element 2 of row 1}\\
\textrm{element 1 of row 2} & \textrm{element 2 of row 2}
\end{array} \right]\\
& = \left[ \begin{array}{cc} 34 & \textrm{element 2 of row 1}\\
\textrm{element 1 of row 2} & \textrm{element 2 of row 2}
\end{array} \right]
\end{align*}\]

To find the second element in row \(1\) of matrix \(AB\), we multiply each element in row \(1\) of \(A\) by the corresponding element in column \(2\) of \(B\), then add them altogether.

\[\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)\\
\textrm{element 1 of row 2} & \textrm{element 2 of row 2}
\end{array} \right]\\
& = \left[ \begin{array}{cc} 34 & 12+4+10\\
\textrm{element 1 of row 2} & \textrm{element 2 of row 2}
\end{array} \right]\\
& = \left[ \begin{array}{cc} 34 & 26\\
\textrm{element 1 of row 2} & \textrm{element 2 of row 2}
\end{array} \right]
\end{align*}\]

To find the first element in row \(2\), we multiply each element in row \(2\) of \(A\) by the corresponding element in column \(1\) of \(B\), then add them together.

\[\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) & \textrm{element 2 of row 2}
\end{array} \right]\\
& = \left[ \begin{array}{cc} 34 & 26\\
7+24+36 & \textrm{element 2 of row 2}
\end{array} \right]\\
& = \left[ \begin{array}{cc} 34 & 26\\
67 & \textrm{element 2 of row 2}
\end{array} \right]
\end{align*}\]

Finally, to find the second element in row \(2\), we multiply each element in row \(2\) of \(A\) by the corresponding element in column \(2\) of \(B\), then add them together.

\[\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*}\]

Consider matrices \(A\) and \(B\).
\[A = \left[ \begin{array}{cc} 2 & 1\\
3 & 0
\end{array} \right]\quad
B = \left[ \begin{array}{ccc} 2 & 1 & 0\\
0 & 2 & 1
\end{array} \right] \] Find, if they exist:

  1. \(AB\)
  2. \(BA\)
  3. \(AA\)
  4. \(BB\).

\(A\) is \(2\times2\) and \(B\) is \(2\times3\), so \(AB\) will be a \(2\times3\) matrix.

\[\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*}\]

\(BA\) cannot be defined since the number of columns in \(B\) is \(3\) and the number of rows in \(A\) is \(2\). They are unequal, so \(BA\) does not exist.

\(A\) is a square matrix. Since it has the same number of columns and rows, we can complete the matrix multiplication, i.e. \(AA\) exists or is defined.

\[\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*}\]

\(B\) is not a square matrix. Its number of columns and rows is unequal, so \(BB\) is undefined. It does not exist.

Consider matrices \(A\) and \(B\).
\[A = \left[ \begin{array}{cc} 2 & -1\\
1 & 3
\end{array} \right]\quad
B = \left[ \begin{array}{cc} 1 & 0\\
2 & -1
\end{array} \right] \] Find, if they exist:

  1. \(AB\)
  2. \(BA\).

Since the number of columns and rows is the same for matrices \(A\) and \(B\), both \(AB\) and \(BA\) exist. The products of both will be \(2\times2\) matrices.

\[\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*}\] \[\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 most cases, \(AB\neq BA\). We say that matrix multiplication is not commutative and the order of multiplication matters. There is an exception to this: when multiplying by a unit matrix.

Your turn – multiplying matrices

  1. Consider the following matrices.
    \[\begin{align*} A & =\left[ \begin{array}{cc} 4 & -1\\
    1 & 3
    \end{array} \right]\quad
    B = \left[ \begin{array}{cc} 1 & 3\\
    -2 & -4\\
    2 & 0
    \end{array} \right]\quad
    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]\quad
    E = \left[ \begin{array}{ccc} -3 & 2 & 0\\
    0 & 5 & -1
    \end{array} \right]
    \end{align*}\] Find, if they exist:
    1. \(BA\)
    2. \(DB\)
    3. \(CD\)
    4. \(AA=A^{2}\)
    5. \(BB=B^{2}\)
    6. \(AD\)
    7. \(BE\)
    8. \(EA\)
    9. \(CB\)

    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\\
      -2\\
      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] \)

Multiplying by the unit matrix

Remember that a square matrix with \(1\)'s along its leading diagonal and zeros elsewhere is called a unit or identity matrix. The order of the unit matrix is sometimes noted with a subscript. For example:

\[\begin{align*} I_{1} & = \left[1\right]\quad
I_{2} = \left[ \begin{array}{cc} 1 & 0\\
0 & 1
\end{array} \right]\quad
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]\quad
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 \(1\) in the real number system. When you multiply by the unit matrix, the original matrix remains unchanged.

When you multiply a square matrix \(n\times n\) by a unit matrix \(I_{n}\), the order of multiplication does not matter. That is:
\[AI_{n}=I_{n}A\]

Example – multiplying by the unit matrix

Consider matrix \(A\).
\[A = \left[ \begin{array}{cc} 1 & 2\\
3 & 4
\end{array} \right]\] Show that \(AI\) is the same as \(IA\).

Since \(A\) has two columns, we need the unit matrix to have two rows and two columns. Therefore, \(I=\left[\begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right]\).
\[\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*}\] \[ \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, \(AI=IA=A\).

Images on this page by RMIT, licensed under CC BY-NC 4.0


Further resources

Introduction to matrices

Matrix multiplication requires you to be able to identify the shape or order of a matrix. Use this resource to review this concept.


Keywords