4
$\begingroup$

Suppose $x_1,\dots,x_n \in \mathbb{R}^d$. Is there a vectorized way of representing the square distance matrix $D_{ij} = \lVert x_i - x_j \rVert^2$?

$\endgroup$
  • $\begingroup$ Nothing elegant, I'm afraid. The best I know is the first code block of this answer. $\endgroup$ – user357151 Apr 18 '17 at 19:08
4
$\begingroup$

We could make use of $\|x_i-x_j\|^2=\|x_i\|^2+\|x_j\|^2-2(x_i \cdot x_j)$, the Gramian matrix $G=[\langle x_i, x_j\rangle]$, its diagonal elements vector $g=\operatorname{diag}(G)$, and the vector of all ones $\mathbf{1}$ to write $$ D = g\mathbf{1}^T + \mathbf{1}g^T - 2G. $$

$\endgroup$
  • $\begingroup$ This is a bit late, but I did happen to come up with this one. Thanks though! $\endgroup$ – user369210 Apr 23 '17 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.