How can I check which range a number falls into for all elements in a cuda tensor without a for loop which is very slow?

For example, I have torch.tensor([1,3,5,7]) and I want to get a tensor that tells me whether it is falling into a 0-2 range or 2-4 range or 4-6 range or 6-8 range. If 0-2, return 0, 2-4, return 1, etc.

Hence, this will give me torch.tensor([0,1,2,3]). Is there an efficient vectorize method to do this kind of stuff?

I know there is apply_ function but it only works with cpu tensor. Is there an equivalent method for cuda tensor?

I think an integer division by two should do the trick at least in this case

something like

tmp = torch.tensor([1,3,5,7]) 
tmp2 = tmp.div(2)

(and if you do not only got a LongTensor: tmp2 = tmp2.trunc())

However in a more general case you could simply generate some masks (I know its still a for loop but should be quite faster than accessing every single value):

range_mins = [2,4,6]
mask = torch.zeros_like(tmp)
for _min in range_mins:
    mask = mask.add((tmp > _min).long())

ok that is a bad example i am looking to know which range a number falls into in the cosine range (1, -1) and there are negative numbers involved. I can’t just divide.

But the example with masking should still work. Maybe your logical queries get a bit more complicated but in general it should work this way

ok gonna try it. Thanks a lot.