Keep off-diagonal elements only from square matrix

I have a (square) pairwise distances matrix, say n x n, and I would like to get rid of the diagonal and hence work with a n x (n - 1) matrix. What’s a fast way of doing that?

One way is a.masked_select(~torch.eye(n, dtype=bool)).view(n, n - 1) but I was curious if there’s a faster approach.

1 Like

I do not know if it is really faster, but just for the fun of it you could try as an alternative:

a.flatten()[1:].view(n-1, n+1)[:,:-1].reshape(n, n-1)