Python | Numpy matrix.itemset ()



With the Numpy matrix.itemset () method we can set elements in a given matrix, just Numpy matrix .itemset () index number and item.

Syntax: matrix.itemset (index, item)

Return: Return new matrix having item

Example # 1:
In this example, we can see that we can set an element using the matrix.itemset () index number and element.

# import an important module into python

import numpy as np

 
# make a matrix with NumPy

gfg = np.matrix ( `[6, 1; 2, 3] ` )

  
# applying the matrix.itemset () method

gfg.itemset (( 1 , 0 ), 5 )

 

print (gfg)

Exit:

 [[6 1] [5 3]] 

Example # 2:

# import an important module into python

import numpy as np

 
# make a matrix with NumPy

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

  
# applying the matrix.itemset () method

gfg.itemset (( 2 , 0 ), 10 )

  

print (gfg)

Exit:

 [[1 2 3] [4 5 6] [10 8 9]]