Elementwise logical comparison of numpy arrays

早过忘川 提交于 2019-12-11 09:27:36

问题


I have two numpy arrays of the same shape. The elements in the arrays are random integers from [0,N]. I need to check which (if any) of the elements in the same position in the arrays are equal.

The output I need are the positions of the same elements.

mock code:

A=np.array([0,1])
B=np.array([1,0])
C=np.array([1,1])
np.any_elemenwise(A,B)
np.any_elemenwise(A,C)
np.any_elemenwise(A,A)

desired output:

[]
[1]
[0,1]

I can write a loop going through all of the elements one by one, but I assume that the desired output can be achieved much faster.


回答1:


EDIT:The question changed.

You just want to evaluate np.where(v1==v2)[0]



来源:https://stackoverflow.com/questions/22605554/elementwise-logical-comparison-of-numpy-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!