![]() |
VOOZH | about |
Sometimes we need to find the sum of the Upper right, Upper left, Lower right, or lower left diagonal elements. Numpy provides us the facility to compute the sum of different diagonals elements using numpy.trace() and numpy.diagonal() method.
Method 1: Finding the sum of diagonal elements using numpy.trace()
Syntax : numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)
Example 1: For 3X3 Numpy matrix
Output:
👁 ImageExample 2: For 4X4 Numpy matrix
Output:
👁 ImageMethod 2: Finding the sum of diagonal elements using numpy.diagonal()
Syntax :
numpy.diagonal(a, offset=0, axis1=0, axis2=1
Example 1: For 3X3 Numpy Matrix
Output:
👁 ImageExample 2: For 5X5 Numpy Matrix