Solved by ptrblck in post #11 
              
              
                Thanks for the update. 
I came up with another approach, but you will only see a slightly performance benefit for larger sizes. 
Your code seems to be alright for this workload: 
def fun1(a, b):
    c = torch.cat((a, b), dim=0)
    idx = c.nonzero()
    res = torch.zeros(c.size()[1:])
    res[idx[:,…