Search code examples
pythonnumpyindices

Retrieve indices of matching points in another array


Total blank on how to do this.

I have an array "points" and an array "a" containing some of those points:

points = numpy.array([[1,2],[4,0],[3,0],[2,4]])
a = numpy.array([[1,2], [3,0]])

How can I get the indices in "points" of points in a ([0,2]), preferably without loops?


Solution

  • This is an O(n²) operation and is not meant to have a direct method without a loop.

    The following solution works for any size of 2D variables of points and a.

    import numpy as np
    points = np.array([[1,2],[4,0],[3,0],[2,4]])
    a = np.array([[1,2], [3,0]])
    
    
    np.array([np.argwhere((points==a[i]).all(axis=1))[0][0] for i in xrange(a.shape[0])])
    # returns array([0, 2])
    

    However, this does not work for values of a missing from points.