Creating a boolean mask for two 2D arrays representing coordinates of 3D points

Viewed 53

I have two arrays, A is an (m, 3)-shape array and B is an (n, 3)-shape array with m > n (this condition is always satisfied. In fact m is at least 3 times n). The two arrays look like this:

A = [[x1  y1  z1]                    |  B = [[u1  v1  w1] 
     [x2  y2  z2]                    |       [u2  v2  w2] 
     ...                             |       ...        
     ...                             |       [un  vn  wn]]   
     ...                             |
     ...                             |
     [xm  ym  zm]]                   |

(u, v, w) and (x, y, z) are coordinates of 3D points. All of the points in B exist in A (which contains more points, most are not in B). One thing to note is that the order of appearance of points in B is not necessarily the same in A, meaning that, for the sake of illustration, [u1 v1 w1] can be at position (row) 256, [u2 v2 w2] at 15, [u3 v3 w3] at 569001, etc.

Another important detail: the coordinates are floats, and if two points correspond, then uk = xj, vk = yj, wk = zj (meaning that the points have the exact position, and are not just close to each other).

I want to create a Boolean mask with the same size as A that will be used later in a code, so that mask_A = [True, False, False, False, True,..., False] where True means that the point also exists in B, and False is put when the point does not appear in B.

I also want to do with it without any loops, because the sizes are huge and the procedure of creating the mask is repeated multiple times. I have no problem changing the type of the arrays (turning them into lists, sets, whatever...) as long as it gets the job done and quickly.

I have found multiple answers dealing with masks and 2D arrays, but none of them answer my question.

Thank you.

1 Answers

As both of them are numpy arrays, you can use the function numpy.intersect1d

_, indices_ar1, _ = numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=True)

I don't know you application but maybe this is enough for you. If not you just have to convert these indices to the binary mask.

Related