Matrices

Matrix Multiplication: How to Multiply Matrices Step by Step

The entry in row ii, column jj of ABAB is ∑kaikbkj\sum_k a_{ik}b_{kj}: row ii of AA dotted with column jj of BB. The product exists only when AA has as many columns as BB has rows.

(AB)ij=∑k=1naik bkj(AB)_{ij} = \sum_{k=1}^{n} a_{ik}\,b_{kj}

The rule looks arbitrary until you see what it encodes, and then it is hard to forget. This page covers the mechanics, the size check, the traps with signs and order, a fast way to catch arithmetic slips, and what the rule is really doing.

How Do You Multiply Matrices? The Row-by-Column Rule

Pick one row from the left matrix and one column from the right matrix. Multiply matching entries, add the results, and write the sum in the product at that row and that column. The picture to hold is “across, then down”: move across row ii of AA while moving down column jj of BB.

Example 1. Multiply A=(1234)A=\begin{pmatrix}1&2\\3&4\end{pmatrix} by B=(5678)B=\begin{pmatrix}5&6\\7&8\end{pmatrix}.

AB=(1⋅5+2⋅71⋅6+2⋅83⋅5+4⋅73⋅6+4⋅8)=(19224350)AB=\begin{pmatrix}1\cdot5+2\cdot7&1\cdot6+2\cdot8\\3\cdot5+4\cdot7&3\cdot6+4\cdot8\end{pmatrix}=\begin{pmatrix}19&22\\43&50\end{pmatrix}

Entry by entry: 5+14=195+14=19, 6+16=226+16=22, 15+28=4315+28=43, 18+32=5018+32=50. The top-left entry came from row 1 and column 1, the top-right from row 1 and column 2, and so on. Position in the answer is always (row of AA, column of BB).

The most common trap is multiplying entry by entry, which would give (5122132)\begin{pmatrix}5&12\\21&32\end{pmatrix}. That is a real operation, but it is not matrix multiplication. It feels natural because adding and subtracting matrices does work entry by entry, and that is exactly why the habit leaks across.

When Is Matrix Multiplication Defined? The Size Check

Row times column gives one number, column times row gives a 3×3 matrix
A 1×3 times a 3×1 collapses to 1×1, but a 3×1 times a 1×3 expands to 3×3.

A dot product needs two lists of equal length. Each row of AA has as many entries as AA has columns, and each column of BB has as many entries as BB has rows. So ABAB exists exactly when those two counts agree. If AA is m×nm\times n and BB is n×pn\times p, the product is m×pm\times p.

Read the size in one glance

Write the sizes side by side as (m×n)(n×p)(m\times n)(n\times p). If the inner pair matches, the outer pair is the size of the answer.

Order changes the verdict, not just the answer. A 2×32\times3 matrix times a 3×23\times2 matrix gives a 2×22\times2 matrix, but reversing them gives a 3×33\times3 matrix. A row times a column (1×n1\times n times n×1n\times1) collapses to a single number, while a column times a row (n×1n\times1 times 1×n1\times n) blows up into a full n×nn\times n matrix.

AA BB ABAB BABA
2×32\times3 3×23\times2 2×22\times2 3×33\times3
2×32\times3 2×32\times3 undefined undefined
2×22\times2 2×12\times1 2×12\times1 undefined
3×13\times1 1×31\times3 3×33\times3 1×11\times1

Do this size check before any arithmetic. It takes five seconds and catches the problems that have no answer.

Quick check

AA is 3×23\times2 and BB is 2×52\times5. What size is ABAB, and is BABA defined?

Matrix Multiplication With Negatives and Fractions

Signs are where accurate setups go wrong. Each entry is a sum of products, so work out every product with its sign first, then add. The usual priorities from order of operations practice problems apply inside every entry: products before sums, and a negative times a negative is positive.

Example 2. Multiply A=(2−1034−2)A=\begin{pmatrix}2&-1&0\\3&4&-2\end{pmatrix} by B=(1−3025−1)B=\begin{pmatrix}1&-3\\0&2\\5&-1\end{pmatrix}. The sizes are 2×32\times3 and 3×23\times2, so the answer is 2×22\times2.

c11=2⋅1+(−1)⋅0+0⋅5=2c12=2⋅(−3)+(−1)⋅2+0⋅(−1)=−6−2+0=−8c21=3⋅1+4⋅0+(−2)⋅5=3+0−10=−7c22=3⋅(−3)+4⋅2+(−2)⋅(−1)=−9+8+2=1\begin{aligned}c_{11}&=2\cdot1+(-1)\cdot0+0\cdot5=2\\c_{12}&=2\cdot(-3)+(-1)\cdot2+0\cdot(-1)=-6-2+0=-8\\c_{21}&=3\cdot1+4\cdot0+(-2)\cdot5=3+0-10=-7\\c_{22}&=3\cdot(-3)+4\cdot2+(-2)\cdot(-1)=-9+8+2=1\end{aligned}

So AB=(2−8−71)AB=\begin{pmatrix}2&-8\\-7&1\end{pmatrix}.

For fractions, factor the common denominator out as a scalar, because a scalar slides through a matrix product.

Example 3. Multiply (12131423)\begin{pmatrix}\frac12&\frac13\\\frac14&\frac23\end{pmatrix} by (4236)\begin{pmatrix}4&2\\3&6\end{pmatrix}. Write the first matrix as 112(6438)\frac{1}{12}\begin{pmatrix}6&4\\3&8\end{pmatrix} and multiply the whole numbers first:

(6438)(4236)=(24+1212+2412+246+48)=(36363654)\begin{pmatrix}6&4\\3&8\end{pmatrix}\begin{pmatrix}4&2\\3&6\end{pmatrix}=\begin{pmatrix}24+12&12+24\\12+24&6+48\end{pmatrix}=\begin{pmatrix}36&36\\36&54\end{pmatrix}

Multiplying by 112\frac{1}{12} gives (33392)\begin{pmatrix}3&3\\3&\frac92\end{pmatrix}. Working directly with fractions gives the same thing: the bottom-right entry is 14⋅2+23⋅6=12+4=92\frac14\cdot2+\frac23\cdot6=\frac12+4=\frac92.

Why Does Matrix Multiplication Work This Way?

A matrix is a machine that turns an input vector into an output vector. Multiplying matrices means chaining two machines: (AB)x(AB)\mathbf{x} is defined to equal A(Bx)A(B\mathbf{x}), so BB acts first and AA acts second. The row-by-column rule is not a convention someone picked; it is the only rule that makes this true.

Here is the derivation for 2×22\times2 matrices. Let BxB\mathbf{x} have entries b11x1+b12x2b_{11}x_1+b_{12}x_2 and b21x1+b22x2b_{21}x_1+b_{22}x_2. Feed that into AA and look at the first output entry:

a11(b11x1+b12x2)+a12(b21x1+b22x2)=(a11b11+a12b21)x1+(a11b12+a12b22)x2a_{11}(b_{11}x_1+b_{12}x_2)+a_{12}(b_{21}x_1+b_{22}x_2)=(a_{11}b_{11}+a_{12}b_{21})x_1+(a_{11}b_{12}+a_{12}b_{22})x_2

The coefficients on x1x_1 and x2x_2 are the first row of ABAB: row 1 of AA against column 1 of BB, then row 1 of AA against column 2 of BB. Nothing else could make one machine equal the chain of two.

With 1×11\times1 matrices this is just composing y=axy=ax and y=bxy=bx to get y=abxy=abx, the lines through the origin from slope-intercept form with intercept 00. Matrices extend that idea to many inputs at once. It also explains the size rule: the output of BB must have the same length as the input of AA.

Why Is Matrix Multiplication Not Commutative?

Chaining machines depends on order, just as putting on socks then shoes differs from shoes then socks. In Example 1, AB=(19224350)AB=\begin{pmatrix}19&22\\43&50\end{pmatrix}, but reversing the order gives

BA=(5⋅1+6⋅35⋅2+6⋅47⋅1+8⋅37⋅2+8⋅4)=(23343146)BA=\begin{pmatrix}5\cdot1+6\cdot3&5\cdot2+6\cdot4\\7\cdot1+8\cdot3&7\cdot2+8\cdot4\end{pmatrix}=\begin{pmatrix}23&34\\31&46\end{pmatrix}

The two answers share nothing. Some pairs do commute, such as any two diagonal matrices of the same size, or any matrix with the identity, but you can never assume it.

Try this: Let A=(1234)A=\begin{pmatrix}1&2\\3&4\end{pmatrix} and S=(0110)S=\begin{pmatrix}0&1\\1&0\end{pmatrix}. Compute ASAS and SASA. One swaps the columns of AA and the other swaps the rows. Which is which, and why does the side matter?

The damage shows up in algebra you would otherwise do on autopilot:

  • (A+B)2=A2+AB+BA+B2(A+B)^2=A^2+AB+BA+B^2, which equals A2+2AB+B2A^2+2AB+B^2 only when AB=BAAB=BA.
  • AB=0AB=0 does not force A=0A=0 or B=0B=0.
  • AB=ACAB=AC does not let you cancel AA to get B=CB=C.

The second point surprises people. Take A=(1000)A=\begin{pmatrix}1&0\\0&0\end{pmatrix} and B=(0001)B=\begin{pmatrix}0&0\\0&1\end{pmatrix}. Every row of AA meets every column of BB in zeros, so ABAB is the zero matrix even though neither factor is.

Quick check

Let A=(1101)A=\begin{pmatrix}1&1\\0&1\end{pmatrix} and B=(1011)B=\begin{pmatrix}1&0\\1&1\end{pmatrix}. Find ABAB and BABA.

How Can You Check a Matrix Product Quickly?

Column sums of A times B equal column sums of AB, worked on Example 1
For A and B from Example 1, the row (4, 6) times B gives (62, 72), exactly the column sums of AB.

Recomputing the same entries tends to repeat the same slip. A different route catches it. The column-sum check is the fastest one: adding the rows of AA together and then multiplying by BB must give the column sums of ABAB.

  1. Confirm the sizes: inner numbers match, outer numbers give the answer’s shape.
  2. Add up each column of AA to get one row of column sums.
  3. Multiply that row by BB. The result must equal the column sums of your answer.

Test it on Example 1. The column sums of AA are (4, 6)(4,\,6). Then (4, 6)B=(4⋅5+6⋅7, 4⋅6+6⋅8)=(62, 72)(4,\,6)B=(4\cdot5+6\cdot7,\ 4\cdot6+6\cdot8)=(62,\,72). The columns of AB=(19224350)AB=\begin{pmatrix}19&22\\43&50\end{pmatrix} add to 19+43=6219+43=62 and 22+50=7222+50=72. They match.

It works because multiplication distributes over addition, so summing the rows of ABAB is the same as summing the rows of AA first. On Example 2 the column sums of AA are (5, 3, −2)(5,\,3,\,-2), and multiplying by BB gives (5−10, −15+6+2)=(−5, −7)(5-10,\ -15+6+2)=(-5,\,-7). The columns of (2−8−71)\begin{pmatrix}2&-8\\-7&1\end{pmatrix} add to −5-5 and −7-7. Correct again.

The check catches any single wrong entry, though two errors that cancel within one column can slip through. For 2×22\times2 matrices a second independent check is available: compare det⁡(AB)\det(AB) with det⁡A⋅det⁡B\det A\cdot\det B, covered below.

Identity, Zero and Inverse Matrices in Products

The identity matrix II plays the role of the number 11: AI=IA=AAI=IA=A whenever the sizes fit. The zero matrix absorbs everything it touches, A⋅0=0A\cdot0=0. Between them sits the inverse: A−1A^{-1} is the matrix with AA−1=A−1A=IAA^{-1}=A^{-1}A=I, and it exists only for square matrices with a nonzero determinant.

Powers need square matrices

A2A^2 means AAAA, so AA must be n×nn\times n. A 2×32\times3 matrix has no square, because the inner sizes 33 and 22 clash.

Multiplication is how you verify an inverse. For A=(2153)A=\begin{pmatrix}2&1\\5&3\end{pmatrix} the determinant is 2⋅3−1⋅5=12\cdot3-1\cdot5=1, so the inverse swaps the diagonal and negates the other two entries: (3−1−52)\begin{pmatrix}3&-1\\-5&2\end{pmatrix}. This is the recipe from the inverse of a 2×2 matrix. Now multiply:

(2153)(3−1−52)=(6−5−2+215−15−5+6)=(1001)\begin{pmatrix}2&1\\5&3\end{pmatrix}\begin{pmatrix}3&-1\\-5&2\end{pmatrix}=\begin{pmatrix}6-5&-2+2\\15-15&-5+6\end{pmatrix}=\begin{pmatrix}1&0\\0&1\end{pmatrix}

Product of inverses reverses order: (AB)−1=B−1A−1(AB)^{-1}=B^{-1}A^{-1}. Undoing “socks then shoes” means taking off the shoes first.

Determinants and Transposes of a Matrix Product

For square matrices of the same size, det⁡(AB)=det⁡A⋅det⁡B\det(AB)=\det A\cdot\det B. Check it on Example 1. The determinant of a 2×2 matrix gives det⁡A=1⋅4−2⋅3=−2\det A=1\cdot4-2\cdot3=-2 and det⁡B=5⋅8−6⋅7=−2\det B=5\cdot8-6\cdot7=-2. For the product, det⁡(AB)=19⋅50−22⋅43=950−946=4\det(AB)=19\cdot50-22\cdot43=950-946=4, which is (−2)(−2)(-2)(-2).

This holds for any size, so with a 3×3 determinant you can find det⁡(AB)\det(AB) without multiplying the matrices at all. You can test your skill with these 3×3 determinant practice problems. It also explains why a product is invertible only when both factors are: if either determinant is 00, so is the product’s.

The transpose reverses the order: (AB)T=BTAT(AB)^T=B^TA^T. With Example 1, (AB)T=(19432250)(AB)^T=\begin{pmatrix}19&43\\22&50\end{pmatrix}, and

BTAT=(5768)(1324)=(19432250)B^TA^T=\begin{pmatrix}5&7\\6&8\end{pmatrix}\begin{pmatrix}1&3\\2&4\end{pmatrix}=\begin{pmatrix}19&43\\22&50\end{pmatrix}

The reversal is forced by sizes. If AA is m×nm\times n and BB is n×pn\times p, then ATBTA^TB^T would be (n×m)(p×n)(n\times m)(p\times n), which usually does not even exist.

Quick check

AA and BB are 2×22\times2 matrices with det⁡A=5\det A=5 and det⁡B=−3\det B=-3. Find det⁡(AB)\det(AB) and det⁡(BA)\det(BA).

Where Is Matrix Multiplication Used in Real Life?

Computer graphics is the cleanest case. Rotating a point counterclockwise by angle θ\theta is multiplication by R(θ)=(cos⁡θ−sin⁡θsin⁡θcos⁡θ)R(\theta)=\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}. For a right angle, R(90∘)=(0−110)R(90^\circ)=\begin{pmatrix}0&-1\\1&0\end{pmatrix}, and doing it twice gives

(0−110)(0−110)=(−100−1)\begin{pmatrix}0&-1\\1&0\end{pmatrix}\begin{pmatrix}0&-1\\1&0\end{pmatrix}=\begin{pmatrix}-1&0\\0&-1\end{pmatrix}

which is a half turn of 180∘180^\circ. Multiply R(a)R(b)R(a)R(b) in general and the top-left entry is cos⁡acos⁡b−sin⁡asin⁡b\cos a\cos b-\sin a\sin b. That equals cos⁡(a+b)\cos(a+b), so the angle-addition formulas are a matrix product in disguise. Different kinds of angles simply give different matrices.

Games and animation stack many such matrices (rotate, scale, shift) into one product, so each point is transformed in a single step. Population models and Markov chains use repeated powers PnP^n to predict where a system settles. Neural networks are layers of matrix products, and search engines rank pages by multiplying huge link matrices again and again.

Cost matters at that scale. Multiplying two n×nn\times n matrices the standard way takes n3n^3 multiplications, so doubling the size makes the work eight times larger. Faster algorithms exist, but the row-by-column rule is still the one that every other method must agree with.

Practice problems

0 of 5 solved

  1. Compute (2013)(142−1)\begin{pmatrix}2&0\\1&3\end{pmatrix}\begin{pmatrix}1&4\\2&-1\end{pmatrix}.

  2. Compute (1230−14)(21−1)\begin{pmatrix}1&2&3\\0&-1&4\end{pmatrix}\begin{pmatrix}2\\1\\-1\end{pmatrix}.

  3. Compute (1213−2)(421−3)\begin{pmatrix}\frac{1}{2}&1\\3&-2\end{pmatrix}\begin{pmatrix}4&2\\1&-3\end{pmatrix}.

  4. Compute (1201−13)(2011−14)\begin{pmatrix}1&2\\0&1\\-1&3\end{pmatrix}\begin{pmatrix}2&0&1\\1&-1&4\end{pmatrix}.

  5. For A=(1234)A=\begin{pmatrix}1&2\\3&4\end{pmatrix}, compute A2−5A−2IA^2-5A-2I.

Common questions

How do you multiply two matrices step by step?

Check that the first matrix has as many columns as the second has rows. Then, for each entry of the answer, multiply matching numbers from one row of the first matrix and one column of the second, and add them. Write the sum at that row and column of the product.

Can you multiply matrices of different sizes?

Yes, as long as the inner sizes match. A 2×32\times3 matrix times a 3×43\times4 matrix works and gives a 2×42\times4 matrix. A 2×32\times3 matrix times another 2×32\times3 matrix does not work.

Is matrix multiplication commutative?

No. ABAB and BABA are usually different, and one of them may not even exist. Matrix multiplication is associative and distributive, so (AB)C=A(BC)(AB)C=A(BC) and A(B+C)=AB+ACA(B+C)=AB+AC.

How many multiplications does a 3×3 times 3×3 product need?

The answer has 99 entries, and each one is a sum of 33 products, so the product needs 2727 multiplications and 1818 additions. For n×nn\times n matrices the count is n3n^3 multiplications.

Can two nonzero matrices multiply to give the zero matrix?

Yes. For example, (1000)(0001)\begin{pmatrix}1&0\\0&0\end{pmatrix}\begin{pmatrix}0&0\\0&1\end{pmatrix} is the zero matrix. Unlike ordinary numbers, a zero product does not force either factor to be zero.

What is the difference between matrix multiplication and entry-by-entry multiplication?

Matrix multiplication combines whole rows with whole columns, so every entry of the answer uses several entries from each factor. Entry-by-entry multiplication (the Hadamard product) multiplies only matching positions and needs matrices of the same size. It is a different operation.

Up next