Tutorials › Linear Algebra › Why Matrix Multiplication Is Not Commutative

Matrix Operations and Algebra · Tutorial 66 of 1000

Why Matrix Multiplication Is Not Commutative

Learn how matrix sizes determine whether AB and BA exist, and how to compare their entries when both products are defined.

Beginner 9 min read

What You'll Learn

  • Determine the dimensions of AB and BA from the dimensions of A and B.
  • Distinguish square matrices of the same order from rectangular matrices.
  • Compute both product orders and compare their entries.
  • Identify commuting pairs without assuming all matrices commute.
  • Correct a common error caused by reversing row and column roles.

Why the Order of a Product Matters

For real numbers, changing the order of two factors does not change their product: \(ab=ba\). Matrix multiplication is different. The product \( \mathbf{A}\mathbf{B} \) combines rows of \(\mathbf{A}\) with columns of \(\mathbf{B}\), while \(\mathbf{B}\mathbf{A}\) uses rows of \(\mathbf{B}\) with columns of \(\mathbf{A}\). Reversing the order changes which entries are paired, and it may even make the product undefined.

Throughout, matrices have real entries. If \(\mathbf{A}\) has \(m\) rows and \(n\) columns, write \(\mathbf{A}\in\mathbb{R}^{m\times n}\). The product \(\mathbf{A}\mathbf{B}\) is defined when the number of columns of \(\mathbf{A}\) equals the number of rows of \(\mathbf{B}\). When it is defined, its size is the number of rows of \(\mathbf{A}\) by the number of columns of \(\mathbf{B}\).

Definition: Two square matrices \(\mathbf{A}\) and \(\mathbf{B}\) of the same size are said to commute if \(\mathbf{A}\mathbf{B}=\mathbf{B}\mathbf{A}\). Equality here means that both products are defined, have the same size, and have equal entries in every position.

For square matrices of the same order, both product orders are defined. That does not mean the products must be equal. The calculations below show both possibilities: one pair gives different products, while another gives the same product in either order.

Reversing the Order Can Change the Product

Worked Example: Two Products That Differ

Let \(\mathbf{A}=\begin{bmatrix}1&2\\0&1\end{bmatrix}\) and \(\mathbf{B}=\begin{bmatrix}1&0\\3&1\end{bmatrix}\). Both are \(2\times2\), so \(\mathbf{A}\mathbf{B}\) and \(\mathbf{B}\mathbf{A}\) are defined and are both \(2\times2\). Compute them separately.

For \(\mathbf{A}\mathbf{B}\), multiply each row of \(\mathbf{A}\) by each column of \(\mathbf{B}\):

$$ \mathbf{A}\mathbf{B} = \begin{bmatrix} 1\cdot1+2\cdot3 & 1\cdot0+2\cdot1\\ 0\cdot1+1\cdot3 & 0\cdot0+1\cdot1 \end{bmatrix} = \begin{bmatrix}7&2\\3&1\end{bmatrix}. $$

For the reverse order, use rows of \(\mathbf{B}\) and columns of \(\mathbf{A}\):

$$ \mathbf{B}\mathbf{A} = \begin{bmatrix} 1\cdot1+0\cdot0 & 1\cdot2+0\cdot1\\ 3\cdot1+1\cdot0 & 3\cdot2+1\cdot1 \end{bmatrix} = \begin{bmatrix}1&2\\3&7\end{bmatrix}. $$

Check: the upper-left entries are \(7\) and \(1\), respectively, so the products cannot be equal. Each displayed entry is the row-column sum for its stated order; in particular, the lower-right entries are \(1\) in \(\mathbf{A}\mathbf{B}\) and \(7\) in \(\mathbf{B}\mathbf{A}\). Thus these matrices do not commute.

This calculation illustrates the essential issue. In \((\mathbf{A}\mathbf{B})_{ij}\), the entries used come from row \(i\) of \(\mathbf{A}\) and column \(j\) of \(\mathbf{B}\). In \((\mathbf{B}\mathbf{A})_{ij}\), they come from row \(i\) of \(\mathbf{B}\) and column \(j\) of \(\mathbf{A}\). The scalar multiplications inside each sum may be familiar, but the entries being multiplied are generally different.

Theorem (Entrywise test for commuting): Let \(\mathbf{A}=[a_{ij}]\) and \(\mathbf{B}=[b_{ij}]\) be in \(\mathbb{R}^{n\times n}\). They commute if and only if, for every \(i,j\) with \(1\le i,j\le n\), \(\sum_{k=1}^{n}a_{ik}b_{kj}=\sum_{k=1}^{n}b_{ik}a_{kj}\).

Proof. Since both matrices are \(n\times n\), both products exist and are \(n\times n\). By the entry formula for matrix multiplication, the entry in row \(i\), column \(j\) of \(\mathbf{A}\mathbf{B}\) is \(\sum_{k=1}^{n}a_{ik}b_{kj}\), while the entry in that position of \(\mathbf{B}\mathbf{A}\) is \(\sum_{k=1}^{n}b_{ik}a_{kj}\). If \(\mathbf{A}\mathbf{B}=\mathbf{B}\mathbf{A}\), equality of matrices requires equality in every entry, giving all the stated equations. Conversely, if all the stated equations hold, the two products agree in every entry. They have the same size, so they are equal matrices. This proves both directions. \(\square\)

The test is a precise way to ask whether a particular pair commutes. It does not claim that every pair of square matrices will pass: one unequal entry is enough to show that the products differ.

Some Pairs Do Commute

Worked Example: A Pair With Equal Products

Consider \(\mathbf{C}=\begin{bmatrix}1&2\\2&1\end{bmatrix}\) and \(\mathbf{D}=\begin{bmatrix}0&1\\1&0\end{bmatrix}\). Both are \(2\times2\). Compute both orders rather than assuming that equality or inequality must occur.

First, use rows of \(\mathbf{C}\) and columns of \(\mathbf{D}\):

$$ \mathbf{C}\mathbf{D} = \begin{bmatrix} 1\cdot0+2\cdot1 & 1\cdot1+2\cdot0\\ 2\cdot0+1\cdot1 & 2\cdot1+1\cdot0 \end{bmatrix} = \begin{bmatrix}2&1\\1&2\end{bmatrix}. $$

Now reverse the order and recalculate the row-column products:

$$ \mathbf{D}\mathbf{C} = \begin{bmatrix} 0\cdot1+1\cdot2 & 0\cdot2+1\cdot1\\ 1\cdot1+0\cdot2 & 1\cdot2+0\cdot1 \end{bmatrix} = \begin{bmatrix}2&1\\1&2\end{bmatrix}. $$

Check: both calculations give \(2\) in positions \((1,1)\) and \((2,2)\), and \(1\) in positions \((1,2)\) and \((2,1)\). The entries agree throughout, so \(\mathbf{C}\mathbf{D}=\mathbf{D}\mathbf{C}\): this pair commutes.

Commuting is a property of a pair of matrices, not a property that can be inferred just from their sizes. The two examples have the same dimensions, and both multiplication orders are defined in both examples. One pair commutes and the other does not; the entries decide which case holds.

When Can Both Orders Be Formed?

For square matrices, the phrase “both orders” has a simple size requirement: the matrices must have the same order. For rectangular matrices, the conditions can be different. The dimensions themselves determine whether the reverse product exists.

Theorem (Dimension conditions for the two orders): Let \(\mathbf{A}\in\mathbb{R}^{m\times n}\) and \(\mathbf{B}\in\mathbb{R}^{p\times q}\). The product \(\mathbf{A}\mathbf{B}\) is defined exactly when \(n=p\), and \(\mathbf{B}\mathbf{A}\) is defined exactly when \(q=m\). Consequently, both products are defined exactly when \(n=p\) and \(q=m\). In that case their sizes are \(m\times m\) and \(n\times n\), respectively.

Proof. By the definition of matrix multiplication, the inner dimensions must agree: the number of columns of the first factor must equal the number of rows of the second. For \(\mathbf{A}\mathbf{B}\), these dimensions are \(n\) and \(p\), so this product is defined exactly when \(n=p\). For \(\mathbf{B}\mathbf{A}\), they are \(q\) and \(m\), so this product is defined exactly when \(q=m\). Both products exist exactly when both conditions hold. Under those conditions, the rows of \(\mathbf{A}\) and columns of \(\mathbf{B}\) make \(\mathbf{A}\mathbf{B}\) an \(m\times q=m\times m\) matrix; the rows of \(\mathbf{B}\) and columns of \(\mathbf{A}\) make \(\mathbf{B}\mathbf{A}\) a \(p\times n=n\times n\) matrix. \(\square\)

This result also clarifies two cases that can otherwise be confused. If \(\mathbf{A}\) and \(\mathbf{B}\) are square but of different orders, neither product is defined: the inner dimensions do not match in either direction. If they are square and the same order, both products are defined, but their entries still need to be compared. For rectangular matrices, even when both orders are defined, their resulting sizes need not match, so equality as matrices is possible only when those sizes match as well.

Worked Example: Only One Product Is Defined

Let \(\mathbf{A}=\begin{bmatrix}1&0&2\\-1&3&1\end{bmatrix}\), which is \(2\times3\), and \(\mathbf{B}=\begin{bmatrix}2&1&0&1\\1&-1&2&0\\0&2&1&-1\end{bmatrix}\), which is \(3\times4\). Since the three columns of \(\mathbf{A}\) match the three rows of \(\mathbf{B}\), \(\mathbf{A}\mathbf{B}\) is defined and has size \(2\times4\).

Compute its entries row by row:

$$ \begin{aligned} \mathbf{A}\mathbf{B} &= \begin{bmatrix} 1\cdot2+0\cdot1+2\cdot0 & 1\cdot1+0\cdot(-1)+2\cdot2 & 1\cdot0+0\cdot2+2\cdot1 & 1\cdot1+0\cdot0+2\cdot(-1)\\ (-1)\cdot2+3\cdot1+1\cdot0 & (-1)\cdot1+3\cdot(-1)+1\cdot2 & (-1)\cdot0+3\cdot2+1\cdot1 & (-1)\cdot1+3\cdot0+1\cdot(-1) \end{bmatrix}\\ &= \begin{bmatrix}2&5&2&-1\\1&-2&7&-2\end{bmatrix}. \end{aligned} $$

For the reverse order, \(\mathbf{B}\) has \(4\) columns and \(\mathbf{A}\) has \(2\) rows. Since \(4\ne2\), the inner dimensions do not match, and \(\mathbf{B}\mathbf{A}\) is undefined. Check: the four entries in the first row are \(2,5,2,-1\), obtained from the first row of \(\mathbf{A}\) and the four columns of \(\mathbf{B}\); the second row entries are \(1,-2,7,-2\), obtained from the second row of \(\mathbf{A}\) and those same columns. The resulting \(2\times4\) size agrees with the dimension rule.

Common Mistake: Reusing the First Product’s Entries

A frequent error is to calculate \(\mathbf{A}\mathbf{B}\) and then assume that \(\mathbf{B}\mathbf{A}\) has the same entries because scalar multiplication satisfies \(ab=ba\). This reverses the order of the matrices but fails to change which row and column are used.

For the first worked example, the correct upper-left entry of \(\mathbf{A}\mathbf{B}\) is \(1\cdot1+2\cdot3=7\). A mistaken reverse calculation may reuse that row of \(\mathbf{A}\) and column of \(\mathbf{B}\), and report \(7\) for \((\mathbf{B}\mathbf{A})_{11}\). But \((\mathbf{B}\mathbf{A})_{11}\) uses row 1 of \(\mathbf{B}\), namely \((1,0)\), and column 1 of \(\mathbf{A}\), namely \((1,0)^\top\). Its value is \(1\cdot1+0\cdot0=1\), not \(7\). The correction is to restart the row-column calculation after reversing the factors.

Before comparing two products, check these points in order:

  • Are both products defined?
  • Do the resulting matrices have the same dimensions?
  • Have the entries of each product been calculated using the correct rows and columns?

Only after the first two checks is an equality comparison meaningful. If the products are defined and have matching dimensions, compare their entries; one difference proves they are not equal.

Check Your Understanding

For each question, use the dimensions first and the row-column rule when a product is defined.

  1. For \(\mathbf{P}=\begin{bmatrix}1&1\\0&2\end{bmatrix}\) and \(\mathbf{Q}=\begin{bmatrix}2&0\\1&1\end{bmatrix}\), compute \(\mathbf{P}\mathbf{Q}\) and \(\mathbf{Q}\mathbf{P}\). Do the matrices commute?
  2. Suppose \(\mathbf{A}\) is \(3\times2\) and \(\mathbf{B}\) is \(2\times3\). Are both product orders defined, and what are the sizes of the two products?
  3. Suppose \(\mathbf{C}\) is \(2\times4\) and \(\mathbf{D}\) is \(3\times2\). Which, if either, of \(\mathbf{C}\mathbf{D}\) and \(\mathbf{D}\mathbf{C}\) is defined?
  4. State the entry formula for \((\mathbf{A}\mathbf{B})_{ij}\) when \(\mathbf{A}\) has \(n\) columns and \(\mathbf{B}\) has \(n\) rows. What changes for \((\mathbf{B}\mathbf{A})_{ij}\)?
  5. Explain why equal dimensions ensure that both orders are defined for square matrices, but do not ensure that the two products are equal.

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon