English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NumPy Matrix Library (Matrix)

A M × N matrix is a rectangular array consisting of M rows (row) and N columns (column).

The elements in the matrix can be numbers, symbols, or mathematical expressions.

matlib.empty()

matlib.empty() function returns a new matrix, syntax format is:

numpy.matlib.empty(shape, dtype, order)

Parameter description:

shape: Defines the integer or integer tuple of the new matrix shape Dtype: Optional, data type order: C (row-major) or F (column-major)

 import numpy.matlib 
 import numpy as np
 print (np.matlib.empty((3,3))

The output result is:

 [[ 2.60605835e-31 -5.21211670e-31 1.30302917e-31]
 [-5.21211670e-31 1.13363538e-30 -3.51817877e-31]
 [ 1.30302917e-31 -3.51817877e-31 2.01969522e-31]]

numpy.matlib.zeros()

numpy.matlib.zeros() function creates a matrix filled with 0.

import numpy.matlib 
import numpy as np
print (np.matlib.zeros((3,3))

The output result is:

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

numpy.matlib.ones()

numpy.matlib.ones() function creates a matrix filled with 1 Filled matrix.

numpy.matlib.zeros() function creates a matrix filled with 0.

import numpy.matlib 
import numpy as np
print (np.matlib.zeros((3,3))

The output result is:

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

numpy.matlib.eye()

numpy.matlib.eye() function returns a matrix, the diagonal elements of which are 1, other positions are zero.

numpy.matlib.eye(n, M, k, dtype)

Parameter description:

n: Returns the number of rows of the matrix M: Returns the number of columns of the matrix, default is n k: Index of the diagonal dtype: Data type

import numpy.matlib 
import numpy as np
print (np.matlib.eye(n = 3, M = 4, k = 0, dtype = float))

The output result is:

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

numpy.matlib.identity()

The numpy.matlib.identity() function returns a unit matrix of a given size.

The unit matrix is a square matrix, and the elements on the diagonal from the top left to the bottom right (known as the main diagonal) are 1All other elements are 0.

import numpy.matlib 
import numpy as np
print(np.matlib.identity(5, dtype = float))

The output result is:

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

numpy.matlib.rand()

The numpy.matlib.rand() function creates a matrix of a given size with randomly filled data.

import numpy.matlib 
 import numpy as np
 print(np.matlib.rand(3,3))

The output result is:

   [[0.32547795 0.58224179 0.87177046]
 [0.83941411 0.43408716 0.43073829]
 [0.39730874 0.84168031 0.99831525]]

Matrices are always two-dimensional, while ndarray is an n-dimensional array. Both objects are interchangeable.

import numpy.matlib 
 import numpy as np
 i = np.matrix('1,2;3,4') 
 print(i)

The output result is:

   [[1 2] 
 [3 4]]
import numpy.matlib 
 import numpy as np
 j = np.asarray(i) 
 print(j)

The output result is:

   [[1 2] 
 [3 4]]
import numpy.matlib 
 import numpy as np
 k = np.asmatrix(j) 
 print(k)
   [[1 2] 
 [3 4]]