Remove all occurences of an item from LongTensor

Is there an efficient way of removing occurences of items in a LongTensor
For example

#input
x = torch.LongTensor([0,2,3,1,4,2,7,1,8])

#output
x = torch.LongTensor([0,3,4,7,8])

In the example above I want to remove 2 and 1 from the tensor. Is there any method in pytorch for this? or any efficient way?

maybe,

x[x.ge(3) + x.le(0)]

or

x[~((x!=1) ^ (x!=2))]
1 Like

Hi and thanks @vainaijr. The two methods works well. Do you have an idea of how I can get the values between 2 and 1 as a longtensor. For example in this case, the output would be x = [torch.LongTensor([3]), torch.LongTensor([7])]? 2 and 1 can be seen as a kind of boundary marker.