<s>[INST] Finding all matrices $B$ such that $AB=BA$ for a fixed matrix $A$ Let 
$$ A=\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
3 & 1 & 2
\end{pmatrix}
 $$
Find all matrices $B$ such that $AB=BA$.
Attempt at solution: I can show that $A$ is invertible so its inverse must be one of the elements. But how do I go about showing there are more of them? or not?. I can set set up the unknown matrix to be a matrix with 9 unknowns and then (at least in principle) try to solve or this system. But I think this is not a very productive way to do this. If these were $2 \times 2$ matrces that would be ok. How should I proceed? Any hints?
Thanks for your time and help. 
 [/INST] $$A=\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
3 & 1 & 2
\end{pmatrix}$$
$$A=\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
-3 & -1 & 1
\end{pmatrix}\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
0 & 0 & 2
\end{pmatrix}\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
3 & 1 & 1
\end{pmatrix}$$
This is the diagonalization of $A$, $A=PDP^{-1}$. Any diagonal matrix in place of $D$ gives a matrix that commutes with $A$. Using $B =PD_0P^{-1}$ :
\begin{align}
 AB  &= PDP^{-1}PD_0P^{-1} \\
   &= PDD_0P^{-1} & \text{($P^{-1}P = I$)}\\
  & = PD_0DP^{-1} &\text{(since diagonal matrices commute)}\\
  & = PD_0P^{-1}PDP^{-1} \\
  & = BA
\end{align}
Lets now look at the full variable form:
$$\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
-3 & -1 & 1
\end{pmatrix}\begin{pmatrix}
\lambda_1 & 0 & 0 \\
0& \lambda_2 & 0 \\
0 & 0 & \lambda_3
\end{pmatrix}\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
3 & 1 & 1
\end{pmatrix}
 = \begin{pmatrix}
\lambda_1 & 0 & 0 \\
0& \lambda_2 & 0 \\
-3\lambda_1 & -\lambda_2 & \lambda_3
\end{pmatrix}\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
3 & 1 & 1
\end{pmatrix}$$
$$\begin{pmatrix}
\lambda_1 & 0 & 0 \\
0& \lambda_2 & 0 \\
-3\lambda_1 & -\lambda_2 & \lambda_3
\end{pmatrix}\begin{pmatrix}
1 & 0 & 0 \\
0& 1 & 0 \\
3 & 1 & 1
\end{pmatrix}= \begin{pmatrix}
\lambda_1 & 0 & 0 \\
0& \lambda_2 & 0 \\
-3\lambda_1+3\lambda_3 & -\lambda_2 + \lambda_3& \lambda_3
\end{pmatrix}$$
This gives $B$ as a function of the three variables $\lambda_1, \lambda_2$, and $\lambda_3$:
$$B=\begin{pmatrix}
\lambda_1 & 0 & 0 \\
0& \lambda_2 & 0 \\
-3\lambda_1+3\lambda_3 & -\lambda_2 + \lambda_3& \lambda_3
\end{pmatrix}$$
And this general $B$ commutes with $A$.
</s>