TITLE: When is matrix multiplication commutative?
QUESTION [130 upvotes]: I know that matrix multiplication in general is not commutative. So, in general:
$A, B \in \mathbb{R}^{n \times n}: A \cdot B \neq B \cdot A$
But for some matrices, this equations holds, e.g. A = Identity or A = Null-matrix $\forall B \in \mathbb{R}^{n \times n}$.
I think I remember that a group of special matrices (was it $O(n)$, the group of orthogonal matrices?) exist, for which matrix multiplication is commutative.
For which matrices $A, B \in \mathbb{R}^{n \times n}$ is $A\cdot B = B \cdot A$?

REPLY [3 votes]: Another commuting example:
ANY two square matrices that, are inverses of each other, commute.
      A B    =  I
inv(A)A B    =  inv(A)   # Premultiplying  both sides by inv(A)
inv(A)A B A  =  inv(A)A  # Postmultiplying both sides by A
        B A  =  I        # Canceling inverses

QED
There are lots of "special cases" that commute.
The multiplication of two diagonal matrices, for example.
Aside: for any two square invertible matrices, A, B, there is something
that can be said about  AB vs. BA
If      AB  =  C
then    BA  =  inv(A) C A  =  B C inv(B)

(Proof: substitute AB for C in the result, and cancel inverses)