Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numpy find identical element in two arrays

Suppose I have an array a and b, how can I find the identical element in both arrays?

a = np.array([[262.5, 262.5, 45],
              [262.5, 262.5, 15],
              [262.5, 187.5, 45],
              [262.5, 187.5, 15],
              [187.5, 262.5, 45],
              [187.5, 262.5, 15],
              [187.5, 187.5, 45],
              [187.5, 187.5, 15]])

b = np.array([[262.5, 262.5, 45],
              [262.5, 262.5, 15],
              [3,3,5],
              [5,5,7],
              [8,8,9]])

I tried the code below, but the output is not what I want, can anyone tell me what is wrong with this code? or is there any other way to do it?

out = [x[(x == b[:,None]).all(1).any(0)] for x in a]

The output I want is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array[[262.5, 262.5, 45],
      [262.5, 262.5, 15]]

>Solution :

a[np.all([np.isin(ai, b) for ai in  a], axis=1)]

or also:

b[np.all([np.isin(bi, a) for bi in  b], axis=1)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading