# Detect overlapping rows in N x 4 tensors when N is large

Hi!
I have 2 large tensors, about [100K x 4] each and I need to find overlapping “rows”. The limiting factors are that rows are not in the same location, e.g., [1000, 45, 23, 0] might be 100th element in one and 125th element in another tensor AND tensors are not of equal length, e.g. one might be [101000 x 4] and other [89500 x 4], actual lengths vary from tensor to tensor. An example where there is one duplicate row `[5,6,7,8]`:

``````a = torch.tensor([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])

b = torch.tensor([[11,22,33,44],
[55,65,57,58],
[95,10,11,124],
[5,6,7,8]])
``````

I have a simple loop-based solution to get the indices of matching rows:

``````matching = []
for coord in a:
result = (b == coord).all(dim=1).nonzero()
if result.shape[0] > 0:
matching.append(result.item())
print("Matching indices in B:", matching)
``````

This works well but is painfully slow for 100k x 100k comparisons. I also have one constraint that might be helpful - it is guaranteed that there are no duplicate rows in a or b, i.e., each row is unique within a same tensor. Any ideas on how to speed this thing up?

Background for understanding my use case: I have somewhere between 0 to 1000 matching rows in tensors (I am working with 3D data and merging point clouds from different sensors so the rows of [x,y,z,time] are unique integer coordinates + integer timestep) and I need to locate rows that are located on the same coordinates at the same time before merging and inputting them into my network.