You could use torch.gather for this:
M = 10
y = torch.empty(M, 10, dtype=torch.long).random_(10)
x = torch.empty(M, dtype=torch.long).random_(10)

w = torch.gather(y, 1, x.unsqueeze(1))
w = w.view(-1)

z = torch.zeros(M, dtype=torch.long)
for k in range(M):
    z[k] = y[k, x[k]]

print((z == w))