matrix operations | identity function ()



numpy.matlib.identity() — another function to do matrix operations in numpy. Returns the square matrix of identity of the given input size.

Syntax: numpy.matlib.identity (n, dtype = None)

Parameters :
n: [int] Number of rows and columns in the output matrix.
dtype: [optional] Desired output data -type.

Return: nxn matrix with its main diagonal set to one, and all other elements zero.

Code # 1:

# Python program explaining
# numpy.matlib .identity () function

 
# import matrix library from numpy

import numpy as geek

import n umpy.matlib

 
# desired output 3 x 3 square matrix

out_mat = geek.matlib.identity ( 3

print ( "Output matrix:" , out_mat) 

Output:

 Output matrix: [[1. 0. 0.] [0 . 1. 0.] [0. 0. 1.]] 

Code # 2:

# Python program explaining
# numpy.matlib.identity () function

 
# import numpy library and matr ocy

import numpy as geek

import numpy.matlib

 
# desired output 5 x 5 square matrix

out_mat = geek.matlib.identity (n = 5 , dtype = int

print ( "Output matrix:" , out_mat) 

Output:

 Output matrix: [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]]