Delete elements according to conditional expression

Hi,

Here i have tensor
a = torch.tensor(([2,0,3],[2,1,3],[2,3,3],[2,2,3],[3,2,3],[2,5,3],[2,8,3]))

i need to delete elements which have 3>= and <6 in the middle … [2,3,3],[2,5,3]
and to get
torch.tensor(([2,0,3],[2,1,3],[2,2,3],[3,2,3],[2,8,3]))

How can i make it ?

Thanks in advance.

Create a masking tensor like below and use that to index:

mask = (a[:, 1] >= 3) & (a[:, 1] < 6)
a[~mask]

#tensor([[2, 0, 3],
#        [2, 1, 3],
#        [2, 2, 3],
#        [3, 2, 3],
#        [2, 8, 3]])
1 Like

Hi akt42,
thank you for your reply !
your advice seems nice & smart.
i will be out during this weekend. i will let you know after trial.

Hi akt42,
It works perfectly !! I really appreciate your advice.

Actually, it took me sometime to check what the ‘~’ is and to review usage of bools.

This will be used to eliminate FP data of YOLOv5 object detection results (results.xyxy[0]) based on x-position.

Thank you very much for your contribution.

1 Like