This might be a really obvious question so I apologize in advance, but I'm having trouble seeing when matrices are commutative for general nxn matrices. For example, when proving tr(AB)=tr(BA), I can easily prove this in a 2x2 matrix but I'm getting confused for proving it in a nxn matrix.
I've searched online and the recurring solution that comes up is:
$$ \Sigma_{i=1}^{n} (AB)_{ii} $$ $$ = \Sigma_{i=1}^{n} \Sigma_{k=1}^{n} A_{nk}B_{kn} $$ $$ = \Sigma_{k=1}^{n} \Sigma_{i=1}^{n} B_{kn}A_{nk} $$ $$ = \Sigma_{i=1}^{n} (BA)_{ii} $$
How come we're able to switch the sums in line 3? I originally tried solving the question by trying to do TR(AB) and TR(BA) separately:
$$ \mathrm{tr}(AB) = \Sigma_{i=1}^{n} (AB)_{ii} = \Sigma_{i=1}^{n} \Sigma_{k=1}^{n} A_{nk}B_{kn} $$
$$ \mathrm{tr}(BA) = \Sigma_{i=1}^{n} (BA)_{ii} = \Sigma_{i=1}^{n} \Sigma_{k=1}^{n} B_{nk}A_{kn} $$
I would greatly appreciate it if someone could perhaps point out where my reasoning went wrong.