如何根据矩阵中的值返回列和行号?
import numpy as np
matrix = np.random.randint(9, size=(3,3))
#matrix
#[[2 3 3]
# [6 2 4]
# [1 8 4]]
类似
matrix.where(2,[]) => (0,0), (1,1)
matrix.where(4,[]) => (1,2), (2,2)
答案 0 :(得分:2)
使用np.argwhere
功能
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)
输出:
array([[1, 0],
[2, 1],
[2, 2]], dtype=int64)
答案 1 :(得分:1)
使用np.where
功能。
matrix = np.random.randint(9, size=(3,3))
np.where(matrix == 2)