简体   繁体   中英

How to do element-wise comparison between two NumPy arrays

I have two arrays. I would like to do an element-wise comparison between the two of them to find out which values are the same.

a= np.array([[1,2],[3,4]])
b= np.array([[3,2],[1,4]])

Is there a way for me to compare these two arrays to 1) find out which values are the same and 2) get the index of the same values?

Adding on to the previous question, is there a way for me to return 1 if the values are the same and 0 otherwise?

Thanks in advance!

a= np.array([[1,2],[3,4]])
b= np.array([[3,2],[1,4]])

#1) find out which values are the same
a==b
# array([[False,  True],
#        [False,  True]])

#2) get the index of the same values?
np.where((a==b) == True) # or np.where(a==b)
#(array([0, 1]), array([1, 1]))

# Adding on to the previous question, is there a way for me to return 1 if the values are the same and 0 otherwise
(a==b).astype(int)
# array([[0, 1],
#        [0, 1]])

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