I have a bigger matrix of dimension
numpy has a
trace() method on its arrays, so you can just use:
list( a[3*m:3*m+3,3*m:3*m+3].trace() for m in range(n) )
Here, the slicing
3*m:3*m+3,3*m:3*m+3 pulls out each submatrix, and then applies the trace to it. This is probably not the most efficient way to do what you want, but it is transparent.