Treating tensor as intervals

Hi,

I have an integer tensor T of shape Nx2 (e.g. [[2,5], [3,7], [1,4]] for N=3) and I want to convert it to a tensor of size NxC such that for every row n, only indices in the interval T[n] are assigned 1 and otherwise 0. I also know that every entry in T is between 0 and C.

Using the example above, when C = 8, the ideal output is:
[[0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 0, 0, 0]].

Is there a way to do this without explicit loops?

I = torch.arange(C,dtype=T.dtype)
bools = (( I >= T [:,0,None]) & ( I <= T[:,1,None])) # broadcasting (1,C) with (N,1)
ints = bools.long()