numpy
  1. numpy-numpymatlibeye

numpy.matlib.eye() - (Matrix Operations with NumPy)

Heading h2

Syntax

numpy.matlib.eye(n, M=None, k=0, dtype=<class 'float'>, order='C')

Example

import numpy.matlib
import numpy as np

array = np.matlib.eye(n = 3, M = 4, k = 0, dtype = float)

print(array)

Output

[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]]

Explanation

The numpy.matlib.eye() function returns a matrix (two-dimensional array) with the diagonals set to one and the remaining elements set to zero. The size and shape of the matrix are specified as arguments to the function.

The n parameter specifies the number of rows in the matrix, while the M parameter specifies the number of columns. If M is not specified, it defaults to n. The k parameter determines which diagonal to set to one. A value of k=0 sets the main diagonal to one, k>0 sets the diagonal above the main diagonal to one, and k<0 sets the diagonal below the main diagonal to one. The dtype parameter specifies the data type of the array.

Use

The numpy.matlib.eye() function is used to create matrices with diagonals set to one and the remaining elements set to zero. This is useful for creating identity matrices, which are square matrices with ones on the main diagonal and zeroes everywhere else. Identity matrices are commonly used in linear algebra.

Important Points

  • The numpy.matlib.eye() function creates matrices with diagonals set to one and the remaining elements set to zero
  • The size and shape of the matrix are specified as arguments to the function
  • The n parameter specifies the number of rows in the matrix, while the M parameter specifies the number of columns
  • The k parameter determines which diagonal to set to one
  • The dtype parameter specifies the data type of the array
  • The function returns a matrix object

Summary

In conclusion, the numpy.matlib.eye() function is a useful tool for creating matrices with diagonals set to one and the remaining elements set to zero. This function is used to create identity matrices, which are commonly used in linear algebra. By specifying the size and shape of the matrix and the location of the diagonal, the numpy.matlib.eye() function provides an easy way to create identity matrices that are necessary in linear algebra computations and other mathematical operations.

Published on: