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}\).
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}\):
For the reverse order, use rows of \(\mathbf{B}\) and columns of \(\mathbf{A}\):
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.
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}\):
Now reverse the order and recalculate the row-column products:
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.
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:
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.
- 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?
- 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?
- 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?
- 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}\)?
- Explain why equal dimensions ensure that both orders are defined for square matrices, but do not ensure that the two products are equal.