How to find the equal element in more than 3 tensors

for example,

a = torch.tensor([[0,1],[2,3]])
b = torch.tensor([[4,1],[2,3]])
c = torch.tensor([[5,1],[8,8]])

since the element with value 1 is equal in all 3 tensors, I want to get the result like

result = torch.tensor([[0,1],[0,0]])


result = torch.tensor([[False,True],[False,False]])

Try this
compare = (a==b)==c
tensor([[False, True],
[False, False]])

Thank you for the suggestion.
I think this happens to be correct because c has value 1 at c[0,1], and a==b returns True at position [0,1].
If they all have, say value 3 at the position [0,1] then this will not work.

What’s more important is I may also want to compare among more than 3 tensors, like choosing equal elements in 5 tensors. In that case, judging one by one may seem too wordy and inefficient.

mask = (torch.stack([a, b, c], dim=0) == a).sum(dim=0) == 3

If a, b, c comes from a larger tensor d = torch.rand(3, 2, 2), do

mask = (d == d[0]).sum(dim=0) == d.shape[0]

Thank you :slight_smile:

Thanks your solution is good and works in all case.
I think below while be better
mask = (torch.stack([b, c], dim=0) == a).sum(dim=0) == 2