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. 
 A: Here's another approach.  Notice that if $A$ and $B$ commute and if $C$ is any invertible matrix, then $C^{-1}AC$ and $C^{-1}BC$ commute.  This is because $$(C^{-1}AC)( C^{-1}BC) = C^{-1}ABC = C^{-1}BAC = (C^{-1}BC)(C^{-1}AC).$$
If we let $C$ be the matrix consisting of eigenvectors of $A$, then one can calculate that $$CAC^{-1} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0\\ 0 & 0& 2\end{bmatrix}.$$
So, what commutes with $CAC^{-1}$?  Multiplying a matrix on the left by $CAC^{-1}$ multiplies the rows by $1$, $1$, and $2$ respectively.  Multiplying a matrix on the right by $CAC^{-1}$ multiplies the columns by $1$, $1$, and $2$ respectively.  The only way these can be equal is if the spots which are multiplied by both $1$ and $2$ are $0$.  It follows that all of the matrices which commute with $CAC^{-1}$ have the form $$\begin{bmatrix} a & b & 0\\ c&d&0\\ 0&0&e\end{bmatrix}$$ and one easily checks that all of these do in fact commute with $C^{-1}AC$.
To turn this back into an answer for $A$ (instead of $C^{-1}AC$), multiply this general form on the left by $C$ and on the right by $C^{-1}$.
Doing this on maple, I find that the matrices which commute with $A$ are precisely those of the form $$\begin{bmatrix} a+c & \frac{1}{3}(a+c-b-d) & 0 \\ -3c & -c+d & 0 \\ -3a+3e & -a+b+e & e\end{bmatrix}. $$
A: $$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$.
A: The matrix $A$ has eigenvalues $2,1,1$ and clean eigenvectors (one of them is $[0, 0, 1]$). This allows you to reduce the problem to $\Lambda \tilde{B} = \tilde{B} \Lambda$ where $\Lambda = \text{diag}(2,1,1)$. Since $\Lambda \tilde{B} \Lambda^{-1}$ seems simple enough you might try to directly solve this version.
A general solution seems to be
\begin{align*}
   U \begin{pmatrix}  & 0 & 0 \\ 0 & \\ 0 \end{pmatrix} U^{-1}
\end{align*}
where the unspecified parts are arbitrary and columns of $U$ are eigenvectors of $A$. Here is one possibility for $$U = \begin{pmatrix} 0 & -1/3 & -1/3 \\ 0 & 1 & 0 \\ 1 & 0 & 1\end{pmatrix}$$
