function K = rbf4nn( X, Y, sigsq )
%function K = rbf4nn( X, Y, sigsq )
%
% Computes an rbf kernel matrix of X with Y, using a uniform
% covariance matrix defined as sigsq*I.
%
%INPUTS
% X = a matrix containing all samples as columns.
% Y = another matrix containing samples as columns.
% sigsq = sigma, the kernel width.
%
%NOTE: does not include the normalization factor!
%
%OUTPUTS
% K_ij = exp(-1/(2*sigsq)*(X_i' * Y_j))
%
% Adapted by D. Wingate from code written by Tijl De Bie.
K = X'*Y;
Xsq = X .* X;
Xsum = sum( Xsq, 1 );
Ysq = Y .* Y;
Ysum = sum( Ysq, 1 );
K = K - Xsum' * ones(1,length(Ysum))/2;
K = K - ones(length(Xsum),1) * Ysum/2;
K = K ./ sigsq;
K = exp( K );