This should work:
A = torch.arange(5)
B = torch.tensor([[0,-1,-1],[1,2,-1],[2,0,1]])
C = []
for x in range(len(B)):
    CPrime= []
    for y in range(len(B[x])):
        if B[x][y]!=-1:
            CPrime.append(A[x])
        else:
            CPrime.append(A[-1])
    C.append(torch.stack(CPrime))
C = torch.stack(C)
print(C)


D = A[torch.arange(B.size(0))].unsqueeze(1).expand_as(B).clone()
D[B==-1] = A[-1]
print((C==D).all())
> tensor(True)

You can post code snippets by wrapping them into three backticks ```