Specific value in a row and a column from that row and the rest is zero


is there a way to this by not using loops?

There might be a cleaner way to do it, but a first attempt:

>>> y = torch.tensor([[2], [7], [1], [2]])
>>> y_idx = torch.zeros(y.shape[0], 10).float()
>>> y_idx.index_put_((torch.arange(4), y.squeeze()), torch.ones(4))
tensor([[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
        [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]])

thank you for this, going to read about index_put_, there should be a lot of solutions but i want to avoid using loops as much as possible.