function K = rbf4( X, Y, sigsq )
%function K = rbf4( 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.
%
%OUTPUTS
% K_ij = n * exp(-1/(2*sigsq)*(X_i' * Y_j))
%
% where n is the standard Gaussian normalizer.
%
% 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 );
K = K .* ( 1/sqrt( (2*pi*sigsq)^size(X,1) ) );