Section 4.2 Multiplication
The multiplication of matrices in linear algebra is not quite as straight-forward as the addition and subtraction are. If you have never heard of matrix multiplication before then it may be helpful for you to quickly watch a few YouTube videos to get up to speed. Matrix multiplication is not an essential skill in this class but it’ll help to put what comes next in context. Also, you’ll need to familiarize yourself with matrix multiplication for future classes, so you might just as well get started now.
The linear algebra style array multiplication is performed in MATLAB with the
*
operator. In order to be able to multiply two matrices it is important that the number of columns of the first matrix is the same as the number or rows of the second matrix. We also say that the inside dimensions must match. Let’s make this precise.If we have two matrices, \(A\) (dimensions: \(m\times p\)) and \(B\) (dimensions: \(p\times n\)), then because their inside dimensions are both \(p\text{,}\) they can be multiplied together to create a matrix \(C = A\times B\) whose dimensions will be \(m\times n\text{.}\)
Unlike the multiplication of numbers, matrix multiplication is not commutative. In other words, in general, \(A * B \neq B * A\)
Rather than writing down the entire general definition of matrix multiplication, we’ll present it for the product of a \(2\times 3\) matrix with a \(3\times 2\) matrix. Since the inside dimensions are both \(3\text{,}\) we can multiply these two and the result will be a \(2\times 2\) matrix. Here is how it’s done: If
\begin{equation*}
A = \begin{bmatrix}
a_{11} & a_{12} & a_{13}\\
a_{21} & a_{22} & a_{23}
\end{bmatrix}
\end{equation*}
and
\begin{equation*}
B = \begin{bmatrix}
b_{11} & b_{12}\\
b_{21} & b_{22}\\
b_{31} & b_{32}
\end{bmatrix}
\end{equation*}
then
\begin{equation*}
A * B =
\begin{bmatrix}
(a_{11}\times b_{11} + a_{12}\times b_{21} + a_{13}\times b_{31}) & (a_{11}\times b_{12} + a_{12}\times b_{22} + a_{13}\times b_{32})\\
(a_{21}\times b_{11} + a_{22}\times b_{21} + a_{23}\times b_{31}) & (a_{21}\times b_{12} + a_{22}\times b_{22} + a_{23}\times b_{32})
\end{bmatrix}
\end{equation*}
Check out the following MATLAB example:
>> A = [8 5 3; 1 2 5]; B = [2 9; 4 6; 3 2]; >> C = A * B
C = 45 108 25 31
In this particular example we could also compute \(B * A\) since the inside dimensions are now both equal to \(2\text{.}\) The result, however, will be a \(3\times 3\) matrix:
>> D = B * A
D =
25 28 51
38 32 42
26 19 19
You can see that definitely, \(A*B\) and \(B*A\) are not the same!
But if the inside dimensions of the two matrices to be multiplied do not agree then they cannot be multiplied. MATLAB will let you know!
>> E = A * A
\(\color{red}{\text{Error using ==> mtimes. Inner matrix dimensions must agree.}}\)