This time to bring you the Python numpy nonzero () should be used, Python numpy in nonzero () use of the notes are what, the following is the actual case, together to see.
The nonzero function returns a directory that is not a 0 element.
The return value is a tuple, with two values of two dimensions, containing the catalog values for the non-0 elements on the corresponding dimension.
Import NumPy as np A = Np.mat ([[[0,1,2,3,4,3,2,1,0],[0,1,2,3,4,5,6,7,0]]) x = A.nonzero () #取出矩阵中的非零元素的坐标 Print x #输出是一个元组, two dimensions. One by one corresponds, #返回非零元素在矩阵中的位置, the previous list holds non-0 line coordinates, and the latter list holds non-0-element column coordinates # (array ([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 ]), Array ([1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7])) #放下来一一对应, i.e. # (array ([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 ] ), #array ([1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7]) # (0,1) (0,2) ...) # (a) # (...)... . (1,6) (1,7) The elements on the coordinates are non-0 print a[x], ' \ n ' #取出矩阵中的非零元素 #[[1 2 3 4 3 2 1 1 2 3 4 5 6 7]] B = Np.mat ([[1, 1, 0,1,0,1,0,0,1],[0,1,1,0,0,0,1,1,1]]) print Np.nonzero (B) # and B.nonzero () equivalent
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
Python how to read and write TXT file line-wise
Python How to implement HMACMD5 encryption algorithm