How to write the distance matrix of a matrix in a matrix algebra form?

48 Views Asked by At

Consider $\boldsymbol{X}_{m \times n}$ consisting $m$ vectors with $n$ dimnesions, I want to write $\boldsymbol{D}_{m \times m}$ which is the matrix whose entries are distances between each two vectors of $\boldsymbol{X}_{m \times n}$. In other words, I want $\boldsymbol{D} = f(\boldsymbol{X})$. I am wondering what's the $f()$?

1

There are 1 best solutions below

0
On BEST ANSWER

Let $\odot$ denote the Hadamard product and $J$ the all-ones matrix with the same dimensions as $X$.

The distance between the $j^{th}$ and $k^{th}$ rows is given by $$\eqalign{ d_{jk}^2 &= \|x_j - x_k\|^2_2 \\ &= x_j^2 + x_k^2 - 2x_j\,x_k \\ &= x_j^2\,{\tt1}_k + {\tt1}_j\,x_k^2 - 2x_j\,x_k \\ }$$ which can be written in matrix form as $$\eqalign{ D\odot D \;=\; (X\odot X)J^T + J(X\odot X)^T - 2XX^T \\\\ }$$ If you want the distances between the columns, it's $$\eqalign{ C\odot C \;=\; (X\odot X)^TJ + J^T(X\odot X) - 2X^TX \\\\ }$$