Solved by ptrblck in post #2 
              
              
                This code should work: 
b, h, w, = 3, 4, 4

A = torch.randint(0, h, (b, 1))
B = torch.randn(b, h, w)
C = B.clone()

idx = torch.zeros(b, h)
idx[torch.arange(b), A.squeeze()] = 1.
idx = idx.cumsum(1).bool()
B[idx] = 0.

for i in range(b):
    C[i, int(A[i]):] = 0

print((B == C).all())
> tensor(True)…