Match rows of two 2D arrays and get a row indices map using numpy

后端 未结 2 769
不思量自难忘°
不思量自难忘° 2020-12-20 05:18

Suppose you have two 2D arrays A and B, and you want to check, where a row of A is contained in B. How do you do this most efficiently using numpy?

E.g.

<         


        
2条回答
  •  心在旅途
    2020-12-20 06:19

    You can take advantage of the automatic broadcasting:

    np.argwhere(np.all(a.reshape(3,1,-1) == b,2))
    

    which results in

    array([[0, 2],
           [1, 0]])
    

    Note for floats you might want to replace the == with np.islclose()

提交回复
热议问题