Python | Numpy matrix.trace ()

NumPy | Python Methods and Functions

Using the Numpy matrix.trace () method, we can find the sum of all elements of the diagonal of a matrix using the matrix.trace () method.

Syntax: matrix.trace ()
Return: Return sum of a diagonal elements of a matrix

Example # 1:
In this example, we see that with matrix.trace () can find the sum of all elements of the diagonal of a given matrix.

# import an important module into python

import numpy as np

 
# make a matrix with NumPy

gfg = np.ma trix ( `[4, 1; 12, 3] ` )

  
# using the matrix.trace () method

geek = gfg.trace ()

 

print (geek)

Output:

 [[7]] 

Example # 2:

# import important module in python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( `[ 4, 1, 9; 12, 3, 1; 4, 5, 6] ` )

  
# using the matrix.trace () method

geek = gfg.trace ()

 

print (geek)

Exit:

 [[13]] 




Get Solution for free from DataCamp guru