简体   繁体   中英

how to get column, row number based on value in matrix from numpy

How to return column and row number based on value in matrix?

import numpy as np

matrix = np.random.randint(9, size=(3,3))
#matrix
#[[2 3 3]
# [6 2 4]
# [1 8 4]]

something like

matrix.where(2,[]) => (0,0), (1,1)
matrix.where(4,[]) => (1,2), (2,2)

Use np.argwhere function

import numpy as np

matrix = np.random.randint(9, size=(3,3))
print(matrix)
# array([[2, 1, 3],
#        [7, 2, 4],
#        [1, 7, 7]])

np.argwhere(matrix == 7)

Output:

array([[1, 0],
       [2, 1],
       [2, 2]], dtype=int64)

Use np.where function.

matrix = np.random.randint(9, size=(3,3))
np.where(matrix == 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM