I have an N-by-M matrix X
, and I need to calculate an N-by-N matrix Y
:
Y[i, j] = sum((X[i,] - X[j,]) ^ 2) 0 <= i,j <= N
For now, I have to use nested loops to do it with O(n2). I would like to know if there's a better way, like using matrix operations.
more generally, sum(....)
can be a function, fun(x1,x 2)
of which x1
, x2
are M-by-1 vectors.