# Matrix Multiplication

You can only multiply two matrices if their dimensions are compatible , which means the number of columns in the first matrix is the same as the number of rows in the second matrix.

If $A=\left[{a}_{ij}\right]$ is an $m×n$ matrix and $B=\left[{b}_{ij}\right]$ is an $n×p$ matrix, the product $AB$ is an $m×p$ matrix.

$AB=\left[{c}_{ij}\right]$ , where ${c}_{ij}={a}_{i1}{b}_{1j}+{a}_{i2}{b}_{2j}+...+{a}_{in}{b}_{nj}$ .

(The entry in the ${i}^{\text{th}}$ row and ${j}^{\text{th}}$ column is denoted by the double subscript notation ${a}_{ij}$ , ${b}_{ij}$ , and ${c}_{ij}$ . For instance, the entry ${a}_{23}$ is the entry in the second row and third column.)

The definition of matrix multiplication indicates a row-by-column multiplication, where the entries in the ${i}^{\text{th}}$ row of $A$ are multiplied by the corresponding entries in the ${j}^{\text{th}}$ column of $B$ and then adding the results.

Matrix multiplication is NOT commutative.   If neither $A$ nor $B$ is an identity matrix, $AB\ne BA$ .

## Multiplying a Row by a Column

We'll start by showing you how to multiply a $1×n$ matrix by an $n×1$ matrix. The first is just a single row, and the second is a single column. By the rule above, the product is a $1×1$ matrix; in other words, a single number.

First, let's name the entries in the row ${r}_{1},{r}_{2},...,{r}_{n}$ , and the entries in the column ${c}_{1},{c}_{2},...,{c}_{n}$ . Then the product of the row and the column is the $1×1$ matrix

$\left[{r}_{1}{c}_{1}+{r}_{2}{c}_{2}+...+{r}_{n}{c}_{n}\right]$ .

Example:

Find the product.

$\left[\begin{array}{rrr}\hfill 1& \hfill 4& \hfill 0\end{array}\right]\cdot \left[\begin{array}{r}\hfill 2\\ \hfill -1\\ \hfill 5\end{array}\right]$

We have to multiply a $1×3$ matrix by a $1×3$ matrix. The number of columns in the first is the same as the number of rows in the second, so they are compatible.

The product is:

$\begin{array}{l}\left[\left(1\right)\left(2\right)+\left(4\right)\left(-1\right)+\left(0\right)\left(5\right)\right]\\ =\left[2+\left(-4\right)+0\right]\\ =\left[-2\right]\end{array}$

## Multiplying Larger Matrices

Now that you know how to multiply a row by a column, multiplying larger matrices is easy. For the entry in the ${i}^{\text{th}}$ row and the ${j}^{\text{th}}$ column of the product matrix, multiply each entry in the ${i}^{\text{th}}$ row of the first matrix by the corresponding entry in the ${j}^{\text{th}}$ column of the second matrix and adding the results.

Let's take the following problem, multiplying a $2×3$ matrix with a $3×2$ matrix, to get a $2×2$ matrix as the product. The entries of the product matrix are called ${e}_{ij}$ when they're in the ${i}^{\text{th}}$ row and ${j}^{\text{th}}$ column.

$\left[\begin{array}{rrr}\hfill 1& \hfill 0& \hfill 1\\ \hfill 0& \hfill 1& \hfill 2\end{array}\right]\cdot \left[\begin{array}{rr}\hfill 3& \hfill 5\\ \hfill -1& \hfill 0\\ \hfill 2& \hfill -1\end{array}\right]=\left[\begin{array}{rr}\hfill {e}_{11}& \hfill {e}_{12}\\ \hfill {e}_{21}& \hfill {e}_{22}\end{array}\right]$

To get ${e}_{11}$ , multiply Row $1$ of the first matrix by Column $1$ of the second.

${e}_{11}=\left[\begin{array}{rrr}\hfill 1& \hfill 0& \hfill 1\end{array}\right]\cdot \left[\begin{array}{r}\hfill 3\\ \hfill -1\\ \hfill 2\end{array}\right]=1\left(3\right)+0\left(-1\right)+1\left(2\right)=5$

To get ${e}_{12}$ , multiply Row $1$ of the first matrix by Column $2$ of the second.

${e}_{12}=\left[\begin{array}{rrr}\hfill 1& \hfill 0& \hfill 1\end{array}\right]\cdot \left[\begin{array}{r}\hfill 5\\ \hfill 0\\ \hfill -1\end{array}\right]=1\left(5\right)+0\left(0\right)+1\left(-1\right)=4$

To get ${e}_{21}$ , multiply Row $2$ of the first matrix by Column $1$ of the second.

${e}_{21}=\left[\begin{array}{rrr}\hfill 0& \hfill 1& \hfill 2\end{array}\right]\cdot \left[\begin{array}{r}\hfill 3\\ \hfill -1\\ \hfill 2\end{array}\right]=0\left(3\right)+1\left(-1\right)+2\left(2\right)=3$

To get ${e}_{22}$ , multiply Row $2$ of the first matrix by Column $2$ of the second.

${e}_{22}=\left[\begin{array}{rrr}\hfill 0& \hfill 1& \hfill 2\end{array}\right]\cdot \left[\begin{array}{r}\hfill 5\\ \hfill 0\\ \hfill 1\end{array}\right]=0\left(5\right)+1\left(0\right)+2\left(-1\right)=-2$

Writing the product matrix, we get:

$\left[\begin{array}{rr}\hfill {e}_{11}& \hfill {e}_{12}\\ \hfill {e}_{21}& \hfill {e}_{22}\end{array}\right]=\left[\begin{array}{rr}\hfill 5& \hfill 4\\ \hfill 3& \hfill -2\end{array}\right]$

Therefore, we have shown:

$\left[\begin{array}{rrr}\hfill 1& \hfill 0& \hfill 1\\ \hfill 0& \hfill 1& \hfill 2\end{array}\right]\cdot \left[\begin{array}{rr}\hfill 3& \hfill 5\\ \hfill -1& \hfill 0\\ \hfill 2& \hfill -1\end{array}\right]=\left[\begin{array}{rr}\hfill 5& \hfill 4\\ \hfill 3& \hfill -2\end{array}\right]$