Find all matrices [3x3] that commute with given matrix [duplicate]

This question already has an answer here:

Find all $3\times 3$ matrices that commute with

$$A =\left( \begin{array}{cc} a_1 & 0 & 0\\ 0 & a_2 & 0\\ 0 & 0 & a_3\end{array} \right)$$

My progress:

I know that a I need to find a matrix such that $AX = XA$. However I'm getting stuck when:

$$AX =\left( \begin{array}{cc} a_1x_{11} & a_1x_{12} & a_1x_{13}\\ a_2x_{21} & a_2x_{22} & a_2x_{23}\\ a_3x_{31} & a_3x_{32} & a_2x_{33}\end{array} \right)$$

$$XA =\left( \begin{array}{cc} a_1x_{11} & a_2x_{12} & a_3x_{13}\\ a_1x_{21} & a_2x_{22} & a_3x_{23}\\ a_1x_{31} & a_2x_{32} & a_3x_{33}\end{array} \right)$$

The answer has been given as:

$$\left( \begin{array}{cc} b_1 & 0 & 0 \\ 0 & b_2 & 0 \\ 0 & 0 & b_3 \end{array} \right)$$

I don't understand how they're getting that form. Can someone please explain?

marked as duplicate by Jack, egreg linear-algebra StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Dec 16 '17 at 15:42

Assuming that all the $a_i$ are different, it's really easy. Just look at each entry in $XA$ and the corresponding entry in $AX$, and you'll see the answer. For instance, we need $a_1x_{12}=a_2x_{12}$, which can only happen if $x_{12}=0$.