kembo
(Ketema Mbogo)
August 14, 2018, 12:36pm
#1
So I have a 1-d tensor T and an index i and need to remove i-th element from a tensor T, much like in pure python T.remove(i).

I’ve tried to do this:

```
i = 2
T = torch.tensor([1,2,3,4,5])
T = torch.cat([T[0:i], T[i+1:-1]])
```

But it fails to bring in the last element (5 in this case).
Any suggestions?

1 Like

fangyh
August 14, 2018, 1:05pm
#2
Update, this works

```
i = 2
T = torch.tensor([1,2,3,4,5])
T = torch.cat([T[0:i], T[i+1:]])
```

1 Like

ptab
August 14, 2018, 1:07pm
#3
Your solution should read
`T = torch.cat([T[0:i], T[i+1:]])`

or equivalently
`T = torch.cat([T[:i], T[i+1:]])`

(but there is probably a better way to do this)

2 Likes

kembo
(Ketema Mbogo)
August 14, 2018, 1:11pm
#5
Thank you guys, this solves the problem.
But there must be a way less clunky solution, I believe.

1 Like

ptrblck
November 22, 2018, 11:39pm
#7
One possible way would be to index the tensor for all other values:

```
x = torch.arange(10)
value = 5
x = x[x!=value]
```

11 Likes

mayank4
(Mayank)
November 23, 2018, 7:19am
#8
I’m getting this error

```
Expected object of scalar type Float but got scalar type Long for argument #2 'other'
```

ptrblck
November 23, 2018, 7:23am
#9
Try to create `value`

as a float type.

SamIIT
(Sam)
December 1, 2020, 9:34pm
#10
This won’t work if `i = 0`

. Any other approach?

How about removing more than 1 elements by indices? Is there a faster way than looping ?

For example

```
i = [0,100,2,5,10, 200,500]
T =torch.randn(1000)
T_new = T [whose elements not equal to elements in i]
```

I want to get the T_new without looping over i…

1 Like

J_Johnson
(J Johnson)
March 3, 2021, 8:39am
#12
@GabbyChan
`T_new = torch.cat([T[:i[0]], T[i[0]+1:i[1]], T[i[1]+1:i[2]],...])`

Does that help?

Not fast enough I am afraid…

J_Johnson
(J Johnson)
March 3, 2021, 1:37pm
#14
If `i`

has a lot of values, a loop is probably your best bet. You could try using multi-processing.

Hello, I think, A more elegant way will be to use a function like:

```
import torch as th
def th_delete(tensor, indices):
mask = th.ones(tensor.numel(), dtype=th.bool)
mask[indices] = False
return tensor[mask]
```

It can be used like this:

```
>>> a = th.arange(5)
>>> a
tensor([0, 1, 2, 3, 4])
>>> th_delete(a, [0, 3, 4])
tensor([1, 2])
>>> th_delete(a, [1])
tensor([0, 2, 3, 4])
>>> th_delete(a, th.tensor([1, 4]))
tensor([0, 2, 3])
```

And even like this:

```
>>> a = th.arange(5)
>>> a
tensor([0, 1, 2, 3, 4])
>>> th_delete(a, 1)
tensor([0, 2, 3, 4])
```

7 Likes

This is an elegant solution. Thanks.

Adex
December 14, 2021, 5:26am
#17

ptrblck:

`x = x[x!=value]`

may you suggest another way to do the same operation if value is not a scaler but another torch 1-d array?

J_Johnson
(J Johnson)
December 14, 2021, 3:12pm
#18
Try this:

```
z=torch.rand(100)
print(z.size())
z=z[z<0.5]
print(z.size())
```

Hey!

What if I wanted to index the tensor for all other values when I want to exclude more than one value?
eg:

```
x = torch.arange(10)
value1 = 5
value2 = 3
```

```
x = x[x!=value and x!=3]
# or:
x = x[x not in [value1, value2]]
```

do not work

ptrblck
September 15, 2022, 5:31am
#20
This should work:

```
x = torch.arange(10)
value1 = 5
value2 = 3
x = x[(x!=value1) & (x!=value2)]
x
# tensor([0, 1, 2, 4, 6, 7, 8, 9])
```

1 Like