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:
$$ \sum_{i=1}^{n} (AB)_{ii} = \sum_{i=1}^{n} \sum_{k=1}^{n} A_{nk}B_{kn} = \sum_{k=1}^{n} \sum_{i=1}^{n} B_{kn}A_{nk} = \sum_{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:
$$ \mbox{tr}(AB) = \sum_{i=1}^{n} (AB)_{ii} = \sum_{i=1}^{n} \sum_{k=1}^{n} A_{nk}B_{kn} $$
$$ \mbox{tr}(BA) = \sum_{i=1}^{n} (BA)_{ii} = \sum_{i=1}^{n} \sum_{k=1}^{n} B_{nk}A_{kn} $$
I would greatly appreciate it if someone could perhaps point out where my reasoning went wrong.
Basically, you have $$(A_{11}B_{11}+A_{12}B_{21}+\ldots)+(A_{21}B_{12}+A_{22}B_{22}+\ldots)+\ldots$$ and by simply regrouping the terms you get $$(A_{11}B_{11}+A_{21}B_{12}+\ldots)+(A_{12}B_{21}+A_{22}B_{22}+\ldots)+\ldots.$$
The products in the sum are products of scalars, thus commutative, and therefore the two double sums are equal.
If you really want to be rigorous about this, you can probably show it by induction.