TL; DR:
I have two pytorch tensors:
t_1 = torch.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
t_2 = torch.Tensor([1, 5, 7])
The desired output is supposed to contain the indices where the elements of t_2
appear in t_1
, i.e.:
output = torch.Tensor([0, 4, 6])
What is critical is that all operations are performed on the GPU, without loops but rather just pytorch operations. The operation itself will be used for very large tensors.
Long version:
The first tensor contains the faces of a triangle mesh. Each face is characterised by 3 indices to individual vertices. There exist F faces, hence the dimensions of the tensor are Fx3
. It contains integers only.
The second tensor has a variable amount of potential faces. Each potential face is also characterised by 3 indices to individual vertices. However, these could be in a different order than in the first tensor. The number of faces changes from iteration to iteration, but is not equal to F, hence Nx3
where N!=F
. It contains integers only.
I want to check, what the index of each potential face of the second array is in the first array. This will help me use pre-computed values, that I have already computed for t_1
.
My current solution relies on calculating a unique value for each triplet in the tensors, thus flattening out the two-dimensional array and arriving at a one-dimensional tensor. Then I can compare these, however, I have not been able to do so efficiently. Furthermore, I have to compute each of the 6 possible combinations of the three indices in each face.
If t_2 and t_1 are already sorted like in the example you can use torch.searchsorted.
torch.searchsorted(t_1, t_2)