Skip to content

How to multiply matrices, step by step

5 min read

Short answer

To multiply AA by BB, each entry of ABAB is the dot product of a row of AA with a column of BB. The product exists only if the number of columns of AA equals the number of rows of BB, and the order matters: in general AB≠BAAB\neq BA.

Matrix size check: 2 by 3 times 3 by 2 gives 2 by 2ABAB2 × 33 × 22 × 2·=inner numbers matchouter numbers = size of AB
Size check: the inner numbers must match (3 and 3). The outer numbers give the size of the answer, 2×2.

Check the sizes first

Write the sizes next to each other: an m×nm\times n matrix times an n×pn\times p matrix. If the two inner numbers are equal, the product exists and has the outer size, m×pm\times p. If they differ, stop: the product is undefined.

The row-times-column rule

Entry (i,j)(i,j) of ABAB is (AB)ij=ai1b1j+ai2b2j+⋯+ainbnj.(AB)_{ij}=a_{i1}b_{1j}+a_{i2}b_{2j}+\cdots+a_{in}b_{nj}. Walk along row ii of AA and down column jj of BB, multiply the pairs and add. Fill the answer one cell at a time, row by row.

A useful way to read the result: each column of ABAB is AA applied to the matching column of BB. That is why matrix multiplication corresponds to doing one transformation after another.

Worked example

Compute ABAB and BABA for A=(1234)A=\begin{pmatrix}1&2\\3&4\end{pmatrix} and B=(0110)B=\begin{pmatrix}0&1\\1&0\end{pmatrix}.

ABAB. Row 1 of AA with column 1 of BB: 1⋅0+2⋅1=21\cdot0+2\cdot1=2. Row 1 with column 2: 1⋅1+2⋅0=11\cdot1+2\cdot0=1. Row 2 with column 1: 3⋅0+4⋅1=43\cdot0+4\cdot1=4. Row 2 with column 2: 3⋅1+4⋅0=33\cdot1+4\cdot0=3. AB=(2143).AB=\begin{pmatrix}2&1\\4&3\end{pmatrix}.

BABA. The same procedure gives BA=(3412).BA=\begin{pmatrix}3&4\\1&2\end{pmatrix}.

BB swaps things: on the right of AA it swaps the columns, on the left it swaps the rows. So AB≠BAAB\neq BA, even for 2×2 matrices.

Try it yourself

Is the product defined? If so, compute it: (102011)(120130)\begin{pmatrix}1&0&2\\0&1&1\end{pmatrix}\begin{pmatrix}1&2\\0&1\\3&0\end{pmatrix}.

Show the answer

Sizes are 2×3 and 3×2, so the inner numbers match and the answer is 2×2. Row 1: 1⋅1+0⋅0+2⋅3=71\cdot1+0\cdot0+2\cdot3=7 and 1⋅2+0⋅1+2⋅0=21\cdot2+0\cdot1+2\cdot0=2. Row 2: 0⋅1+1⋅0+1⋅3=30\cdot1+1\cdot0+1\cdot3=3 and 0⋅2+1⋅1+1⋅0=10\cdot2+1\cdot1+1\cdot0=1. The product is (7231)\begin{pmatrix}7&2\\3&1\end{pmatrix}.

Keep going with practice

Vidi's Linear Algebra & Probability course includes a matrix calculator (determinant, inverse, rank, row reduction) so you can check your own work.