When do two $n \times n$ matrices $A, B$ have the property that $AB = BA$? As we all know that two $n \times n$ matrices $A, B$
need not have the relation $AB = BA.$ But when do two $n \times n$ matrices have such a property? 
 A: To give a completely general answer is too complicated to be of general use. The simplest nice case: if $A$ and $B$ are diagonalizable and share the same eigenspaces, then they commute. This is because sharing the same eigenspaces implies that $A$ and $B$ can be simultaneously diagonalized, i.e. $A=CDC^{-1}$ and $B=CD'C^{-1}$ where $D,D'$ are diagonal and thus commute. This is also a necessary condition among diagonalizable matrices, that is, commuting diagonalizable matrices can be simultaneously diagonalized. 
A: While I am not aware of any necessary and sufficient condition for matrices to commute with $A\in K^{n\times n}$, it is possible to completely characterise the set of matrices that commute with $A\in \mathbb R^{2\times 2}$ (and other special cases) by setting up a system of equations equating the individual coefficients of the two products $AB$ and $BA$ and finding the conditions for those equations to be fulfilled.
For small dimensions, this yields quite usable results. An explicit example for the $2 \times 2$ case can be found here.
