linear

Matrix Multiplication Order: Why Ab Differs From Ba

Matrix multiplication order determines whether a calculation succeeds or fails, yet many learners stumble over why $AB$ does not equal $BA$. When you multiply two matrices, you aren't just scaling num

Matrix Multiplication Order: Why Ab Differs From Ba
In this story

Matrix multiplication order determines whether a calculation succeeds or fails, yet many learners stumble over why $AB$ does not equal $BA$. When you multiply two matrices, you aren't just scaling numbers together. You're mapping linear transformations from one vector space to another, where sequence dictates the final outcome.

If you've ever wondered why swapping matrix positions breaks your code or yields completely different numbers, you're in the right place. We'll break down the rules of matrix dimensions, why commutativity fails, and how to track the order of operations in practical math.

The Core Rule of Matrix Dimensions

Before you can multiply any two matrices, their inner dimensions must match. If matrix $A$ has dimensions $m \times n$, matrix $B$ must have dimensions $n \times p$. That inner dimension $n$ acts as a bridge.

The resulting product matrix, let's call it $C$, inherits the outer dimensions. It will be an $m \times p$ matrix. If those inner numbers don't match, the operation is undefined. You can't force it.

Here's a quick way to check your setup before you start calculating:

  • Write down the dimensions of the first matrix: $m \times n$.
  • Write down the dimensions of the second matrix right next to it: $n \times p$.
  • Check if the inner numbers match. If yes, proceed. The outer numbers give you the shape of your result.

If you attempt to calculate $BA$ instead of $AB$, your dimensions change entirely. If $A$ is $2 \times 3$ and $B$ is $3 \times 4$, then $AB$ gives you a $2 \times 4$ matrix. But for $BA$, you'd need a $3 \times 4$ matrix multiplied by a $2 \times 3$ matrix. The inner dimensions ($4$ and $2$) don't match, meaning $BA$ is mathematically impossible.

Why Matrix Multiplication is Not Commutative

In standard arithmetic, multiplication is commutative. You know that $3 \times 5$ equals $5 \times 3$. Both equal $15$.

Linear algebra doesn't play by those rules. For most matrices, $AB \neq BA$. Even when both products are technically defined—such as when $A$ and $B$ are square matrices of the exact same size—their products usually yield different results.

Why does this happen? Think of matrices as instructions for movement or transformation in space. Suppose matrix $A$ represents a rotation of ninety degrees counterclockwise, and matrix $B$ represents a horizontal stretch.

If you apply transformation $B$ first and then transformation $A$, your final coordinates land in a completely different spot than if you applied $A$ first and then $B$. The sequence matters because each step alters the grid upon which the next step acts.

Step-by-Step Example of AB Versus BA

Let's look at a concrete example using two $2 \times 2$ matrices where both $AB$ and $BA$ are valid.

Let matrix $A$ be: $$A = \begin{pmatrix} 1 & 2 \ 3 & 4 \end{pmatrix}$$

Let matrix $B$ be: $$B = \begin{pmatrix} 2 & 0 \ 1 & 5 \end{pmatrix}$$

To find the top-left entry of the product $AB$, you take the first row of $A$ and the first column of $B$, multiplying corresponding elements and adding them together: $$(1 \times 2) + (2 \times 1) = 2 + 2 = 4$$

When you complete all rows and columns for $AB$, you get: $$AB = \begin{pmatrix} 4 & 10 \ 10 & 20 \end{pmatrix}$$

Now let's calculate $BA$ by putting matrix $B$ first and matrix $A$ second: $$BA = \begin{pmatrix} 2 & 0 \ 1 & 5 \end{pmatrix} \begin{pmatrix} 1 & 2 \ 3 & 4 \end{pmatrix}$$

Computing the top-left entry for $BA$: $$(2 \times 1) + (0 \times 3) = 2 + 0 = 2$$

When you finish all calculations for $BA$, you get: $$BA = \begin{pmatrix} 2 & 4 \ 16 & 22 \end{pmatrix}$$

Look at the results. $AB$ equals $\begin{pmatrix} 4 & 10 \ 10 & 20 \end{pmatrix}$, while $BA$ equals $\begin{pmatrix} 2 & 4 \ 16 & 22 \end{pmatrix}$. They share no resemblance. This disparity highlights why order dictates your entire outcome. You can explore more about foundational algebraic structures at Khan Academy Linear Algebra.

When Does Commutativity Actually Work?

While $AB \neq BA$ is the general rule, there are specific scenarios where matrix multiplication commutes. Recognizing these exceptions saves you time during proofs and derivations.

  • Identity Matrix Interaction: Any square matrix $A$ commutes with the identity matrix $I$ of the same dimension. You'll find that $AI = IA = A$.
  • Scalar Matrices: A scalar matrix is a diagonal matrix where all main diagonal entries are equal to the same scalar value. Multiplying any square matrix by a matching scalar matrix commutes.
  • Inverse Matrices: If $A$ and $B$ are inverses of each other ($AB = I$), then $BA$ also equals $I$.
  • Diagonal Matrices: Two square diagonal matrices of the same size will always commute because their non-zero entries live solely on the main diagonal.

Beyond these exact conditions, assume your matrices won't commute unless a specific problem statement guarantees it.

Real-World Consequences of Wrong Order

Getting your matrix sequence backwards in practical applications leads to severe errors. Software engineers, data scientists, and roboticists deal with this daily.

In computer graphics and game development, rendering a 3D object requires multiplying transformation matrices for scaling, rotation, and translation. If you write your shader code or transformation pipeline in the wrong order, your character might rotate around the wrong axis before scaling, or translate off-screen entirely.

In robotics, forward kinematics rely on multiplying homogeneous transformation matrices to determine the position of a robotic arm's end effector. Changing the order of multiplication means the robot calculates its joint angles based on a completely distorted physical frame of reference. For further technical documentation on numerical computing standards, check out IEEE Computer Society.

Frequently Asked Questions

Can a non-square matrix have a commutative product?

No. If $A$ and $B$ are non-square matrices, $AB$ and $BA$ usually cannot both be defined because their inner and outer dimensions won't align in both directions. Even if they are shaped such that both products exist, their dimensions won't match each other, making equality impossible.

Does the associative property still hold for matrix multiplication?

Yes. While matrix multiplication isn't commutative, it is associative. This means $(AB)C = A(BC)$ is always true, provided the dimensions allow the multiplications to take place. You can group your matrices however you like without changing the final result.

What happens if the product of two non-zero matrices is zero?

In standard arithmetic, if $ab = 0$, then either $a$ or $b$ must be zero. In matrix algebra, you can multiply two non-zero matrices and get a zero matrix. This phenomenon underscores how different matrix arithmetic is from real numbers.

How do I remember which matrix goes first in transformations?

Think of matrix multiplication as reading from right to left. If you apply transformation $B$ to vector $v$, and then apply transformation $A$ to the result, you write it as $A(Bv) = (AB)v$. The matrix closest to the vector acts first.

IC

Written by

Isabella Carter

As a veteran correspondent, Isabella Carter has reported from across the globe, bringing firsthand perspectives to international stories and local issues.

More from linear