Skip to content

Distance Matrices

Sometimes the data is not the points themselves but the distances between them: a table of pairwise separations, with no coordinates attached. A distance matrix records those separations, and the question is whether a set of points realizing them exists and how to recover it. The answer is a clean piece of linear algebra. A short matrix identity converts squared distances into inner products, and the SVD reads the coordinates off the resulting Gram matrix. The companion problem, aligning two known configurations, is solved by the same decomposition.

Let x1,,xnRd\xv_1, \dots, \xv_n \in \R^d be unknown points and suppose we are given only the squared distances Dij=xixj2D_{ij} = \lVert \xv_i - \xv_j \rVert^2. Expanding the square ties each distance to inner products:

Dij=xi22xiTxj+xj2.D_{ij} = \lVert \xv_i \rVert^2 - 2\,\xv_i^{\rm T}\xv_j + \lVert \xv_j \rVert^2 .

The cross term xiTxj\xv_i^{\rm T}\xv_j is exactly the entry GijG_{ij} of the Gram matrix G=XXT\Gv = \Xv\Xv^{\rm T}, where XRn×d\Xv \in \R^{n \times d} stacks the points as rows. The diagonal terms xi2\lVert \xv_i\rVert^2 depend only on the origin, which the distances cannot fix; translating all points leaves D\Dv unchanged. Pinning the origin at the centroid, ixi=0\sum_i \xv_i = \mathbf{0}, removes that freedom and lets us solve for G\Gv.

Once G=XXT\Gv = \Xv\Xv^{\rm T} is known it is symmetric positive semidefinite, and its eigendecomposition hands back the coordinates. Write G=QΛQT\Gv = \Qv\Lambdav\Qv^{\rm T} with eigenvalues λ1λn0\lambda_1 \ge \cdots \ge \lambda_n \ge 0. Then

X=QΛ1/2\Xv = \Qv\,\Lambdav^{1/2}

realizes the distances, and the number of nonzero eigenvalues is the smallest dimension dd in which the points embed exactly. Keeping only the largest few eigenvalues gives the best low-dimensional embedding, by Eckart–Young applied to G\Gv; this is classical multidimensional scaling, the standard way to place points in the plane from a table of dissimilarities.

A squared-distance matrix is double-centered into a Gram matrix, whose eigendecomposition yields point coordinates up to rotation

The pipeline: squared distances D\Dv, double centered to the Gram matrix G=12JDJ\Gv = -\tfrac12\Jv\Dv\Jv, then eigendecomposed to coordinates X=QΛ1/2\Xv = \Qv\Lambdav^{1/2}. The reconstruction is fixed only up to a rigid motion.

The construction also answers the existence question exactly.

The Procrustes problem: aligning two configurations

Section titled “The Procrustes problem: aligning two configurations”

Distances determine points only up to a rigid motion: rotating, reflecting, or translating every point preserves every pairwise distance. So two reconstructions of the same data, or two measured configurations of corresponding points, will in general be misaligned by an orthogonal transformation. Finding the best one to bring them into register is the orthogonal Procrustes problem.

The maximum value iσi\sum_i \sigma_i is the nuclear norm of XTY\Xv^{\rm T}\Yv, and Q=UVT\Qv = \Uv\Vv^{\rm T} is the orthogonal matrix nearest to XTY\Xv^{\rm T}\Yv. If a proper rotation is required, detQ=+1\det\Qv = +1, and the SVD returns a reflection, the standard fix is to negate the column of U\Uv belonging to the smallest singular value, which costs the least in the objective.

Drag either point set below. The widget forms XTY\Xv^{\rm T}\Yv, takes its SVD, and rotates the movable set by Q=UVT\Qv = \Uv\Vv^{\rm T} to land as close as possible on the fixed set; the residual is the Procrustes distance between the two shapes.

drag the blue points
as positioned: target Y, movable X
after the optimal Q = UVᵀ
Optimal rotation -56°, Procrustes residual ‖XcQ − Yc‖_F = 0.156. The SVD of M = Xcᵀ Yc gives Q = UVᵀ, the orthogonal matrix that lands the centred blue shape as close as possible on the accent shape. Drag points to change the residual; only a rigid rotation can be undone, so stretching one cloud leaves error behind.

Distance matrices and Procrustes alignment are the geometric face of the SVD: one recovers a configuration from its internal distances, the other matches two configurations across an orthogonal ambiguity, and both are solved by reading singular values and vectors off a single small matrix.