# Matrix Multiplication

To multiply two matrices, the number of columns of the first matrix must be equal to the number of rows of the second matrix.

## Matrix multiplication algorithm

We multiply the elements on the rows of the first matrix by the elements on the columns of the second matrix.

1. We multiply the elements on the first row by the elements on the first column.
• We multiply the first element of the first row by the first element of the first column.
• We multiply the second element of the first row by the second element of the first column.
• We do this for every element until reaching the end of both the first row of the first matrix and the first column of the second matrix.
• We add up each of the results.
• The final result will be the first element of the first row of the resulting matrix.
2. We multiply the elements of the first row of the first matrix by the elements of the second column of the second matrix.
• We multiply the first element of the first row by the first element of the second column.
• We multiply the second element of the first row by the second element of the second column.
• We do this for every element until reaching the end of both the first row of the first matrix and the second column of the second matrix.
• We add up each of the results.
• The final result will be the second element of the first row of the resulting matrix.
3. Using the same algorithm we multiply the elements of the first row of the first matrix by the elements of the remaining columns of the second matrix. The results will complete the first row of the resulting matrix.
4. The second line of the resulting matrix will be completed in the same manner, by multiplying the elements on the second line of the first matrix by the elements on each column of the second matrix and filling in the results of each addition.
5. We do this for every line of the first matrix until the resulting matrix is complete.

Example 7
$A= \begin{pmatrix} 1 & 2 & 2\\ 3 & 1 & 1 \end{pmatrix}$
$B=\begin{pmatrix} 4 & 2 \\ 3 & 1 \\ 1 & 5\\ \end{pmatrix}$

We notice that matrix A has 3 columns and matrix B has 3 lines, which means we can multiply them.

$A \cdot B=$ $\begin{pmatrix} \color{red}1 &\color{blue}2 & \color{green}2\\ \color{red}3 &\color{blue}1 & \color{green}1 \end{pmatrix} \begin{pmatrix} \color{red}4 & \color{red}2 \\ \color{blue}3 & \color{blue}1 \\ \color{green}1 & \color{green}5 \end{pmatrix}=$ $\begin{pmatrix} \color{red}{1\cdot4}+\color{blue}{2\cdot3}+\color{green}{2\cdot1} & \color{red}{1\cdot2}+\color{blue}{2\cdot1}+\color{green}{2\cdot5}\\ \color{red}{3\cdot4}+\color{blue}{1\cdot3}+\color{green}{1\cdot1} & \color{red}{3\cdot2}+\color{blue}{1\cdot1}+\color{green}{1\cdot5} \end{pmatrix}=$ $\begin{pmatrix} 12 & 14\\ 16 & 12\\ \end{pmatrix}$

$B \cdot A = \begin{pmatrix} \color{red}4 &\color{blue}2 \\ \color{red}3 & \color{blue}1 \\ \color{red}1 & \color{blue}5 \end{pmatrix} \begin{pmatrix} \color{red}1 &\color{red}2 & \color{red}2\\ \color{blue}3 &\color{blue}1 & \color{blue}1 \end{pmatrix}=$

$\begin{pmatrix} \color{red}{4\cdot1}+\color{blue}{2\cdot3} & \color{red}{4\cdot2}+\color{blue}{2\cdot1} & \color{red}{4\cdot2}+\color{blue}{2\cdot1}\\ \color{red}{3\cdot1}+\color{blue}{1\cdot3} & \color{red}{3\cdot2}+\color{blue}{1\cdot1} & \color{red}{3\cdot2}+\color{blue}{1\cdot1}\\ \color{red}{1\cdot1}+\color{blue}{5\cdot3} & \color{red}{1\cdot2}+\color{blue}{5\cdot1} & \color{red}{1\cdot2}+ \color{blue}{5\cdot1} \end{pmatrix} =$ $\begin{pmatrix} 10 & 10 & 10 \\ 6 & 7 & 7 \\ 16 & 7 & 7 \end{pmatrix}$

We notice that $A \cdot B \neq B \cdot A$

Example 8
$A= \begin{pmatrix} 5 & 2 \\ 3 & 1 \end{pmatrix} B= \begin{pmatrix} 4 & 6 \\ 5 & 2 \end{pmatrix}$

$A \cdot B = \begin{pmatrix} \color{red}5 & \color{blue}2 \\ \color{red}3 & \color{blue}1 \end{pmatrix} \cdot \begin{pmatrix} \color{red}4 & \color{red}6 \\ \color{blue}5 & \color{blue}2 \end{pmatrix} =\begin{pmatrix} \color{red}{5\cdot4}+\color{blue}{2\cdot5} & \color{red}{5\cdot6}+\color{blue}{2\cdot2} \\ \color{red}{3\cdot4}+\color{blue}{1\cdot5} & \color{red}{3\cdot6}+\color{blue}{1\cdot2} \end{pmatrix} =$ $\begin{pmatrix} 30 & 34\\ 17 & 20 \end{pmatrix}$

$B \cdot A= \begin{pmatrix} \color{red}4 & \color{blue}6 \\ \color{red}5 & \color{blue}2 \end{pmatrix} \cdot \begin{pmatrix} \color{red}5 & \color{red}2 \\ \color{blue}3 & \color{blue}1 \end{pmatrix} =\begin{pmatrix} \color{red}{4\cdot5}+\color{blue}{6\cdot3} & \color{red}{4\cdot2}+\color{blue}{5\cdot1} \\ \color{red}{5\cdot5}+\color{blue}{2\cdot3} & \color{red}{5\cdot2}+\color{blue}{2\cdot1} \end{pmatrix} =$ $\begin{pmatrix} 38 & 14\\ 31 & 12 \end{pmatrix}$

Once again $A \cdot B \neq B \cdot A$.

Example 9
$A= \begin{pmatrix} 1 & 4 & 3 \\ 2 & 1 & 5\\ 3 & 2 & 1 \end{pmatrix} B= \begin{pmatrix} 5 & 2 & 1 \\ 4 & 3 & 2 \\ 2 & 1 & 5 \end{pmatrix}$

$A \cdot B = \begin{pmatrix} \color{red}{1} & \color{blue}{4} & \color{green}{3} \\ \color{red}{2} & \color{blue}{1} & \color{green}{5}\\ \color{red}{3} & \color{blue}{2} & \color{green}{1} \end{pmatrix} \cdot \begin{pmatrix} \color{red}{5} & \color{red}{2} & \color{red}{1} \\ \color{blue}{4} & \color{blue}{3} & \color{blue}{2} \\ \color{green}{2} & \color{green}{1} & \color{green}{5} \end{pmatrix}=$

$\begin{pmatrix} \color{red}{1\cdot5} + \color{blue}{4\cdot4} + \color{green}{3\cdot2} & \color{red}{1\cdot2} + \color{blue}{4\cdot3} + \color{green}{3\cdot1} & \color{red}{1\cdot1} + \color{blue}{4\cdot2} + \color{green}{3\cdot5} \\ \color{red}{2\cdot5} + \color{blue}{1\cdot4} + \color{green}{5\cdot2} & \color{red}{2\cdot2} + \color{blue}{1\cdot3} + \color{green}{5\cdot1} & \color{red}{2\cdot1} + \color{blue}{1\cdot2} + \color{green}{5\cdot5}\\ \color{red}{3\cdot5} + \color{blue}{2\cdot4} + \color{green}{1\cdot2} & \color{red}{3\cdot2} + \color{blue}{2\cdot3} + \color{green}{1\cdot1} & \color{red}{3\cdot1} + \color{blue}{2\cdot2} + \color{green}{1\cdot5} \end{pmatrix}=$
$=\begin{pmatrix} 27 & 17 & 24\\ 24 & 12 & 29\\ 25 & 14 & 12 \end{pmatrix}$

$B \cdot A = \begin{pmatrix} \color{red}{5} & \color{blue}{2} & \color{green}{1}\\ \color{red}{4} & \color{blue}{3} & \color{green}{2}\\ \color{red}{2} & \color{blue}{1} & \color{green}{5} \end{pmatrix} \cdot \begin{pmatrix} \color{red}{1} & \color{red}{4} & \color{red}{3} \\ \color{blue}{2} & \color{blue}{1} & \color{blue}{5} \\ \color{green}{3} & \color{green}{2} & \color{green}{1} \end{pmatrix}=$ $\begin{pmatrix} \color{red}{5\cdot1} + \color{blue}{2\cdot2} + \color{green}{1\cdot2} & \color{red}{5\cdot4} + \color{blue}{2\cdot1} + \color{green}{1\cdot2} & \color{red}{5\cdot3} + \color{blue}{2\cdot5} + \color{green}{1\cdot1} \\ \color{red}{4\cdot1} + \color{blue}{3\cdot2} + \color{green}{2\cdot3} & \color{red}{4\cdot4} + \color{blue}{3\cdot1} + \color{green}{2\cdot2} & \color{red}{4\cdot3} + \color{blue}{3\cdot5} + \color{green}{2\cdot1}\\ \color{red}{2\cdot1} + \color{blue}{1\cdot2} + \color{green}{5\cdot3} & \color{red}{2\cdot4} + \color{blue}{1\cdot1} + \color{green}{5\cdot2} & \color{red}{2\cdot3} + \color{blue}{1\cdot5} + \color{green}{5\cdot1} \end{pmatrix}=$
$=\begin{pmatrix} 11 & 24 & 26\\ 16 & 23 & 29\\ 19 & 19 & 16 \end{pmatrix}$

Once again $A \cdot B \neq B \cdot A$.

Example 10
$A= \begin{pmatrix} 5 & 2\\ 3 & 1\\ \end{pmatrix} I_{2}= \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ \end{pmatrix}$

$A \cdot B = \begin{pmatrix} \color{red}{5} & \color{blue}{2}\\ \color{red}{3} & \color{blue}{1} \end{pmatrix} \cdot \begin{pmatrix} \color{red}{1} & \color{red}{0} \\ \color{blue}{0} & \color{blue}{1} \end{pmatrix} =\begin{pmatrix} \color{red}{5\cdot1}+\color{blue}{2\cdot0} & \color{red}{5\cdot0}+\color{blue}{2\cdot1} \\ \color{red}{3\cdot1}+\color{blue}{1\cdot0} & \color{red}{3\cdot0}+\color{blue}{1\cdot1} \end{pmatrix} = \begin{pmatrix} 5 & 2\\ 3 & 1 \end{pmatrix}$

$B \cdot A = \begin{pmatrix} \color{red}{1} & \color{blue}{0} \\ \color{red}{0} & \color{blue}{1} \end{pmatrix} \cdot \begin{pmatrix} \color{red}{5} & \color{red}{2} \\ \color{blue}{3} & \color{blue}{1} \\ \end{pmatrix} =\begin{pmatrix} \color{red}{1\cdot5}+\color{blue}{0\cdot3} & \color{red}{1\cdot2}+\color{blue}{0\cdot1} \\ \color{red}{0\cdot5}+\color{blue}{1\cdot3} & \color{red}{0\cdot2}+\color{blue}{1\cdot1} \end{pmatrix} = \begin{pmatrix} 5 & 2\\ 3 & 1 \end{pmatrix}$

We notice that $A \cdot I_{2} = I_{2} \cdot A=A$.

Example 11
$A=\begin{pmatrix} 1 & 4 & 3 \\ 2 & 1 & 5\\ 3 & 2 & 1 \end{pmatrix} I_{3}= \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix}$

$A \cdot B = \begin{pmatrix} \color{red}{1} & \color{blue}{4} & \color{green}{3} \\ \color{red}{2} & \color{blue}{1} & \color{green}{5}\\ \color{red}{3} & \color{blue}{2} & \color{green}{1} \end{pmatrix} \cdot \begin{pmatrix} \color{red}{1} & \color{red}{0} & \color{red}{0} \\ \color{blue}{0} & \color{blue}{1} & \color{blue}{0} \\ \color{green}{0} & \color{green}{0} & \color{green}{1} \end{pmatrix}=$

$\begin{pmatrix} \color{red}{1\cdot1} + \color{blue}{4\cdot0} + \color{green}{3\cdot0} & \color{red}{1\cdot0} + \color{blue}{4\cdot1} + \color{green}{3\cdot0} & \color{red}{1\cdot0} + \color{blue}{4\cdot0} + \color{green}{3\cdot1} \\ \color{red}{2\cdot1} + \color{blue}{1\cdot0} + \color{green}{5\cdot0} & \color{red}{2\cdot0} + \color{blue}{1\cdot1} + \color{green}{5\cdot0} & \color{red}{2\cdot0} + \color{blue}{1\cdot0} + \color{green}{5\cdot1}\\ \color{red}{3\cdot1} + \color{blue}{2\cdot0} + \color{green}{1\cdot0} & \color{red}{3\cdot0} + \color{blue}{2\cdot1} + \color{green}{1\cdot0} & \color{red}{3\cdot0} + \color{blue}{2\cdot0} + \color{green}{1\cdot1} \end{pmatrix}=$
$=\begin{pmatrix} 1 & 4 & 3\\ 2 & 1 & 5\\ 3 & 2 & 1 \end{pmatrix}$ $B \cdot A = \begin{pmatrix} \color{red}{1} & \color{blue}{0} & \color{green}{0} \\ \color{red}{0} & \color{blue}{1} & \color{green}{0}\\ \color{red}{0} & \color{blue}{0} & \color{green}{1} \end{pmatrix} \cdot \begin{pmatrix} \color{red}{1} & \color{red}{4} & \color{red}{3} \\ \color{blue}{2} & \color{blue}{1} & \color{blue}{5} \\ \color{green}{3} & \color{green}{2} & \color{green}{1} \end{pmatrix}=$

$\begin{pmatrix} \color{red}{1\cdot1} + \color{blue}{0\cdot2} + \color{green}{0\cdot2} & \color{red}{1\cdot4} + \color{blue}{0\cdot1} + \color{green}{0\cdot2} & \color{red}{1\cdot3} + \color{blue}{0\cdot5} + \color{green}{0\cdot1} \\ \color{red}{0\cdot1} + \color{blue}{1\cdot2} + \color{green}{0\cdot3} & \color{red}{0\cdot4} + \color{blue}{1\cdot1} + \color{green}{0\cdot2} & \color{red}{0\cdot3} + \color{blue}{1\cdot5} + \color{green}{0\cdot1}\\ \color{red}{0\cdot1} + \color{blue}{0\cdot2} + \color{green}{1\cdot3} & \color{red}{0\cdot4} + \color{blue}{0\cdot1} + \color{green}{1\cdot2} & \color{red}{0\cdot3} + \color{blue}{0\cdot5} + \color{green}{1\cdot1} \end{pmatrix} =$
$=\begin{pmatrix} 1 & 4 & 3\\ 2 & 1 & 5\\ 3 & 2 & 1 \end{pmatrix}$

Once again $A \cdot I_{3} = I_{3} \cdot A = A$.

To conclude:

1. Generally, matrix multiplication is not commutative.
2. $A\cdot I_{n} = I_{n} \cdot A = A$ for any matrix A with n columns.

Contact email: