# Change tensor values by index greater(or less) than some value

Hey everyone!

I have a question of selecting/changing values by some specific indices.
Here is an example:

``````import torch

"""
The original tensor is like this:

t = torch.tensor(
[[99, 2, 1, 3, 4],
[1, 2, 3, 99, 4],
[4, 1, 99, 2, 3]]
)

I want to change t's value with rule:
for each row, set elements which is before 99 to 1; 0 otherwise,

so my target is:

tensor(
[[0, 0, 0, 0],
[1, 1, 1, 0],
[1, 1, 0, 0]]
)

following is the for-loop way, my question is, can I do this more efficiently?

Thanks!

"""

# create a tensor of shape [3, 4]
t = torch.tensor(
[[99, 2, 1, 3, 4],
[1, 2, 3, 99, 4],
[4, 1, 99, 2, 3]]
)

# get 99's position of each row
position_of_zero = torch.nonzero(t == 99)[:, 1]

print(position_of_zero)

# for-loop
for row, zero_pos in zip(t, position_of_zero):
for i in range(zero_pos):
row[i] = 1

for i in range(zero_pos + 1, len(row)):
row[i] = 0

print(t)

# drop 99
t = t[t != 99].view(-1, 4)
print(t)

``````

The desired target doesn’t match the description:

``````for each row, set elements which is before 99 to 1; 0 otherwise,
``````

since the `99` in the first row is set to `0`, while the others are not.
Assuming you would like to set the value at `99` to `1`, this code should work and would avoid the loop:

``````t2 = torch.tensor(
[[99, 2, 1, 3, 4],
[1, 2, 3, 99, 4],
[4, 1, 99, 2, 3]]
)

idx = torch.cumsum((t2 == 99), 1)
idx[t2==99] = 0
res = (~idx.bool()).long()
print(res)
> tensor([[1, 0, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 0, 0]])
``````

Depending on the shape of your tensor, it might be faster or not, so I would recommend to profile both approaches.

since the `99` in the first row is set to `0` , while the others are not.

Sorry for the unclear description, actually I want to just DROP all the 99s,

I slightly modified you solution, and it works for me too!

``````t2 = torch.tensor(
[[99, 2, 1, 3, 4],
[1, 2, 3, 99, 4],
[4, 1, 99, 2, 3]]
)

idx = torch.cumsum((t2 == 99), 1)
idx[t2==99] = 2
idx = idx[idx != 2].view(3, -1)

res = (~idx.bool()).long()
print(res)
> tensor([[1, 0, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 0, 0]])
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.6544346,"math_prob":0.9980802,"size":910,"snap":"2022-05-2022-21","text_gpt3_token_len":351,"char_repetition_ratio":0.11589404,"word_repetition_ratio":0.1734104,"special_character_ratio":0.44395605,"punctuation_ratio":0.2734694,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98847944,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-23T04:34:08Z\",\"WARC-Record-ID\":\"<urn:uuid:4eecfd82-5b04-477f-9040-c204a9f4b9d1>\",\"Content-Length\":\"19768\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:16446fb4-5ae9-44ca-97eb-fee710636a0f>\",\"WARC-Concurrent-To\":\"<urn:uuid:557b8e3c-fa5c-4ed6-9d89-6bb3bd3d0fac>\",\"WARC-IP-Address\":\"159.203.145.104\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/change-tensor-values-by-index-greater-or-less-than-some-value/110885\",\"WARC-Payload-Digest\":\"sha1:6U5IOON576XYCWGXWUMHLVARJQONCFVK\",\"WARC-Block-Digest\":\"sha1:WD4XAWJMHRXSWNK32UBK5RITOAHHK7MI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662555558.23_warc_CC-MAIN-20220523041156-20220523071156-00393.warc.gz\"}"}