Find adjacency matrix given a list of class labels

Hi,
Supposing that I have L = [1 1 1 0 0 2 2], where each number indicates a different class. Using MATLAB, I can write:

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

which gives

A =
     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     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     1     1
     0     0     0     0     0     1     1

Please, how can I do this in pytorch and with no loops?
Thanks

This question is similar to this stackoverflow question

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