Usually, matrices are non-commutative objects. However, I want to know under what conditions matrices do commute, i.e. for a given (invertible) matrix $A$, find the set of all possible $B$ such that
$$AB=BA$$
This is connected to the center of the group. In fact, if we preserve the group structure, the center of $A$ should be the entire group. From the Wikipedia, such case does exists, i.e. for the Heisenberg group representation
$$\begin{pmatrix}1&a&c\\0&1&b\\0&0&1\end{pmatrix}$$, the subset(in this case, also the subgroup) was $$\begin{pmatrix}1&0&c\\0&1&0\\0&0&1\end{pmatrix}$$
Is there a general condition for the set(or group of) matrix $B$ for an arbitrary matrix $A$? Consider the $2\times 2$, $3\times 3$, $4\times 4$, and $5\times 5$. The $1\times 1$ the scalar case was trivally satisfied. For the interests of the physics, also consider $7\times 7$ matrix because ".. if the (cross) product is limited to non-trivial binary products with vector results, it exists only in three and seven dimensions."
What's the condition such that the matrix $AB=BA$?