Skip to content

Instantly share code, notes, and snippets.

@michaelchughes
Created October 24, 2012 22:38
Show Gist options
  • Save michaelchughes/3949403 to your computer and use it in GitHub Desktop.
Save michaelchughes/3949403 to your computer and use it in GitHub Desktop.
N = 100;
M = 4;
sig = .4;
% Create evenly spaced pts between 0 and M
% X is N x 1
X = linspace( 0, M, N )';
% Calc squared Euclidean distance
% between every pair of rows in X
% D is N x N
D = pdist2( X, X, 'euclidean' ).^2;
% Compute Gaussian RBF Kernel matrix
% K is N x N
K = exp( -0.5/(sig^2) * D );
fprintf( 'Min Eig Val of K: %.3e\n', min(eig(K)) );
assert( min( eig(K ) ) >= 0, 'K is not positive semi-definite!' );
fprintf('K is positive definite!\n' );
%%
% We can double check that pdist2 is the "right" distance calculation here.
% but of course, it is.
A = eye(2);
B = 2*eye(2);
D = pdist2( A, B, 'euclidean').^2;
D2 = zeros(2,2);
for a = 1:2
for b = 1:2
diffVec = A(a,:)-B(b,:);
D2(a,b) = sum( diffVec*diffVec' );
end
end
D
D2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment