13.13: Matrix Multiplication
- Page ID
- 135931
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Matrix multiplication is one of the most important operations in linear algebra, but it is also one of the easiest places to make mistakes. Unlike array multiplication, matrix multiplication is not done by simply multiplying matching entries.
In MATLAB, matrix multiplication uses the * operator.
For two matrices \(A\) and \(B\), the product \(A * B\) is allowed only if the number of columns of \(A\) is equal to the number of rows of \(B\).
For example, suppose \(A\)is a 2-by-3 matrix and \(B\)is a 3-by-2 matrix:
A = [3 5 3; 4 2 10]; B = [1 3; 4 9; 7 3];
The product A*B is allowed because the number of columns in A is 3, and the number of rows in B is also 3.
The result will have the number of rows from A and the number of columns from B. So the result will be a 2-by-2 matrix.
C = A * B
You can think of the size rule like this:
A is 2-by-3 B is 3-by-2
The inner dimensions must match:
2-by-3 times 3-by-2
Since the two inner numbers are both 3, the multiplication is allowed.
The outer dimensions tell us the size of the answer:
2-by-2
So, C = A * Bwill produce a 2-by-2 matrix.
How MATLAB Calculates Each Entry
Each entry in the result is found by multiplying a row from the first matrix by a column from the second matrix.
For example:
A = [3 5 3; 4 2 10]; B = [1 3; 4 9; 7 3];
B = [1 3; 4 9; 7 3];
To find the entry in row 1, column 1 of C, MATLAB uses row 1 of A and column 1 of B:
Row 1 of A:
[3 5 3]
Column 1 of B:
[1; 4; 7]
Then MATLAB multiplies corresponding values and adds the results:
3*1 + 5*4 + 3*7 = 3 + 20 + 21 = 44
So the first entry of C is 44.
To find row 1, column 2, MATLAB uses row 1 of A and column 2 of B:
3*3 + 5*9 + 3*3 = 9 + 45 + 9 = 63
So the first row of C is: [44 63]
MATLAB repeats this process for every row-column pair.
Matrix Multiplication Is Not Usually Commutative
For regular numbers, multiplication is commutative. This means:
3*5 = 5*3
But matrix multiplication is usually not commutative. In other words:
A*B is usually not the same as B*A
Sometimes A*B may be allowed, but B*A may not be allowed because the dimensions do not match. Other times both products may be allowed, but the answers may be different.
This is very important in MATLAB. The order of multiplication matters.
Matrix Multiplication vs. Element-by-Element Multiplication
MATLAB has two different multiplication operators for arrays and matrices:
| Operation | MATLAB syntax | Meaning |
|---|---|---|
| Matrix multiplication | A * B |
Linear algebra multiplication using rows and columns |
| Element-by-element multiplication | A .* B |
Multiply matching entries |
Use * when you are doing linear algebra.
Use .* when you want to multiply matching elements in two arrays.
Matrix multiplication vs. element-by-element multiplication
Solution
Pay attention to how different the results are!
C =
19 22
43 50
D =
5 12
21 32
Why Matrix Multiplication Matters
Matrix multiplication is used in many engineering and scientific applications. For example, it can be used to:
represent systems of linear equations,
transform points in computer graphics,
- rotate or scale objects,
- model electrical circuits,
- solve force balance problems,
- analyze data sets,
- and perform calculations in machine learning and numerical methods.

