Suppose we have a matrix A composed of m vectors with n dimensions.
And suppose we want to get the averaged Euclidean distance between all of those vectors. I know how to do this in for loops but I am not quite as clear for matrix operations.
I see someone posed the question here and received an answer:
Average distance in distance matrix - Cross Validated
How would this be implemented in Pytorch as a matrix operation?
Additionally, how would it need to be modified if the matrix is complex(i.e. torch.cfloat)?
So I found torch.cdist creates a Euclidean distance matrix.
So getting the average shouldn’t be an issue.
I noticed cdist doesn’t support complex matrices. But that’s fine. I can just do this:
A = torch.rand(n_vectors, vector_dims, dtype=torch.cfloat)
B = torch.cdist(A.real, A.real)
C = torch.cdist(A.imag, A.imag)
E_dist = (B**2 + C**2)**0.5
Avg_dist = torch.sum(E_dist) / (n_vectors*(n_vectors-1)))