NumPy - the sum of the elements of the secondary diagnosis of a two-dimensional matrix

How can I get the sum of the elements on the secondary diagonal of a matrix? numpy.trace

seems to only return the main diagonals and numpy.diagonal

doesn't help with the secondary diagonal either.

+3


source to share


1 answer


You can always just flip the array a

(top to bottom) and use np.trace

:



a[::-1].trace()

      

+8


source







All Articles