Python| numpy matrix.nonzero()
借助Numpy matrix.nonzero()
方法,我们可以从给定矩阵中获取非零值的索引值。它总是给我们二维数组。
Syntax : matrix.nonzero()
Return : Return index value of nonzero from given matrix
示例 #1:
在这个例子中,我们可以看到我们能够在方法matrix.nonzero()
的帮助下从给定的矩阵中获取非零的索引值。
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix('[64, 1; 0, 3]')
# applying matrix.nonzero() method
geeks = gfg.nonzero()
print(geeks)
输出:
(array([0, 0, 1]), array([0, 1, 1]))
示例 #2:
# import the important module in python
import numpy as np
# make a matrix with numpy
gfg = np.matrix('[11, 0, 3; 34, 0, 65; 7, 68, 0]')
# applying matrix.nonzero() method
geeks = gfg.nonzero()
print(geeks)
输出:
(array([0, 0, 1, 1, 2, 2]), array([0, 2, 0, 2, 0, 1]))