NumPy - Sum of the elements on the secondary diagnoal of a 2D matrix

2.6k views Asked by At

How can I get the sum of the elements on the secondary diagonal of a matrix? numpy.trace seems to only return main diagonals, and numpy.diagonal doesn't seem to help out with secondary diagonal either.

1

There are 1 answers

3
Alex Riley On BEST ANSWER

You could always just flip the array a (top to bottom) and use np.trace:

a[::-1].trace()