t = tensor([[1,3,2,6]])
rows = t.shape[1]
cols = t.max() + 1
output = torch.zeros(rows, cols) # initializes zeros array of desired dimensions
output[list(range(rows)), t.tolist()] = 1 # sets cells to 1
To clarify the last operation, you can pass in a list of the row numbers and column numbers, and it will set all those elements to the value after the equal.
So in our case we want to set the following locations to 1:
(0,1), (1,3), (2,2), (3,6)
Which we'd represent as:
output[[0,1,2,3], [1,3,2,6]] = 1
And you'll see that those lists line up with a) an increasing list up to the total row count, and b) our original tensor