Solved by will-leeson in post #6 
              
              
                Thanks so much. I had to make a slight change since A is multidimensional. Here is what I ended up with: 
D = A[torch.arange(B.size(0))].unsqueeze(1).expand(-1, B.size(1),-1).clone()
D[B==-1]=torch.full_like(A[0], -float("inf"))