user3073333 user3073333 -4 years ago 142
Python Question

To find trace of sub-matrices in python

I have a bigger matrix of dimension

. I need to find trace of each
of dimension 3x3. I have found answers on SO for splitting the bigger matrix into smaller
but I can't find a way to store them and find trace for each submatrices in
. Any suggestion would be of immense help. Thank you in advance.

Answer Source

Well, 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download