target = torch.tensor([1.25, 2.5, 3, 1.25, 3])
values = torch.tensor([1.25, 2.5, 3])
I would like to find a fast operation that maps every values in target
by their indices in values
:
result: torch.tensor([0, 1, 2, 0, 2])
Thanks
target = torch.tensor([1.25, 2.5, 3, 1.25, 3])
values = torch.tensor([1.25, 2.5, 3])
I would like to find a fast operation that maps every values in target
by their indices in values
:
result: torch.tensor([0, 1, 2, 0, 2])
Thanks
I convert this problem to a "finding equal values in two expanded matrixes’. Here is the code:
target = torch.tensor([1.25, 2.5, 3, 1.25, 3])
values = torch.tensor([1.25, 2.5, 3])
t_size = target.numel()
v_size = values.numel()
t_expand = target.unsqueeze(1).expand(t_size, v_size)
v_expand = v_size.unsqueeze(0).expand(t_size, v_size)
result = (t_expand - v_expand == 0).nonzero()[:,1]