jpainam:

A = zeros(numel(L));
A = bsxfun(@eq,L,L.');
A(~L,~L) = 0;



Even this way doesn’t work as expected
L = torch.tensor([1, 1, 1, 0, 0, 2, 2])
A = (L == L.view(-1, 1)).long()
tensor([[1, 1, 1, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 0, 0],
        [0, 0, 0, 1, 1, 0, 0],
        [0, 0, 0, 0, 0, 1, 1],
        [0, 0, 0, 0, 0, 1, 1]])
T = ~ (L != 0)
A[T, T] = 0
tensor([[1, 1, 1, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0, 0],
        [0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 1],
        [0, 0, 0, 0, 0, 1, 1]])
>>> 

which is a little bit different.
Any help will be useful