ptrblck:

a = torch.tensor([1, 0, 0, 0, 1, 0]) b = torch.tensor([0, 0, 1, 0, 0, 0]) res = ((a==1) & (b==0)).long() idx = ((a==0) & (b==0)).nonzero() res[idx] = res[idx-1] print(res) > tensor([1, 1, 0, 0, 1, 1])


Thanks a lot for your answer. I knew I had to be somehow unclear on my intentions :). My problem is that I want the “equal to previous value” mechanism to be recursive.
Hence
a = torch.tensor([1, 0, 0, 0, 0, 0, 0, 1, 0])
b = torch.tensor([0, 0, 0, 0, 0, 1, 0, 0, 0])

would result in
> tensor(1, 1, 1, 1, 1, 0, 0, 1, 1)

To answer your first question, it’s assumed either a or b will equal 1 in first pos.