Search multiple 1D numpy-arrays in 3D numpy-array

Let’s try broadcasting to compare all the triangle to all the points:

compare = (Triangle[:,:,None,:] == points[None,None,...])

# `all` check if all the coordinates to be equal,
# `sum` counts the equal points 
mask = compare.all(axis=-1).sum(axis=(-2,-1)) >=2

Triangle[mask]

Output:

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

       [[1, 2, 3],
        [0, 1, 1],
        [1, 1, 2]],

       [[1, 2, 3],
        [0, 2, 1],
        [0, 1, 1]],

       [[0, 3, 1],
        [0, 2, 1],
        [1, 3, 2]],

       [[0, 2, 1],
        [1, 2, 3],
        [1, 3, 2]],

       [[1, 1, 2],
        [2, 1, 2],
        [2, 2, 1]],

       [[2, 2, 1],
        [1, 2, 3],
        [1, 1, 2]],

       [[2, 3, 1],
        [2, 2, 1],
        [3, 2, 1]],

       [[2, 3, 1],
        [1, 3, 2],
        [1, 2, 3]],

       [[1, 2, 3],
        [2, 2, 1],
        [2, 3, 1]]])

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top