Given a matrix ( numpy
array) A
and a vector v
.
A = np.array([[5,0],[1,-2],[0,2],[-1,3],[1,2]])
v = np.array([0,2])
What is the best way to get the index of the vector v in the matrix A (in this case one should get 2).
这样做:
np.argwhere((v == A).all(1))
Out[82]: array([[2]], dtype=int64)
If by best you mean fastest , after thorough experimentation user hpaulj pointed out that np.flatnonzero
is a much faster alternative to np.argwhere
. You can use it this way:
np.flatnonzero((v==A).all(1))[0]
Output:
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.