Skip to content

Instantly share code, notes, and snippets.

@gyk
Created August 28, 2014 09:45
Show Gist options
  • Save gyk/08c53041f4f6667875d9 to your computer and use it in GitHub Desktop.
Save gyk/08c53041f4f6667875d9 to your computer and use it in GitHub Desktop.
% The inner matrix can be computed by double centering the
% squared distance matrix.
% This script verifies this inner matrix actually refers to the
% centroid of all points as the origin.
approxeq = @(A, B) sum(sum(abs(A - B))) < 1e-6;
nDims = 5;
n = 4;
X = rand(nDims, n);
innerp = X' * X;
mag2 = sum(X .^ 2);
D2 = bsxfun(@plus, mag2', mag2) - 2 * innerp;
D = D2 .^ 0.5;
H = eye(n) - 1/n;
doubleCentered = H * D2 * H;
centered = bsxfun(@minus, bsxfun(@minus, D2, mean(D2)), ...
mean(D2, 2)) + mean(D2(:));
assert(approxeq(doubleCentered, centered));
% inner product matrix by double centering
innerp_dc = -1/2 * doubleCentered;
X_m = bsxfun(@minus, X, mean(X, 2));
innerp_m = X_m' * X_m;
assert(approxeq(innerp_dc, innerp_m));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment