function  outKernel = getGaussianKernel(inMat, inSigma)

% calculate the Gaussian kernel given the inMat
% input : inMat: p x n
%       : inSigma : scalar 
% output : outKernel : n x n

% centerize data first
% inMat = inMat - repmat(mean(inMat, 2), 1, size(inMat, 2));
n = size(inMat, 2);
% centerize
% inMat = inMat - repmat(mean(inMat, 2), 1, size(inMat, 2));
% calculate the pairwise Euclidian dist
Dist_square = L2_distance_1(inMat, inMat);
Dist = Dist_square.^(0.5);
Dist = real(Dist);
d = diag(Dist);
% default sigma = sum_{i, j} (||x_i - x_j||/n(n-1))
if( nargin < 2)
    inSigma = sum(sum(Dist - diag(d)))/(n*(n-1));
end

outKernel = exp(-((Dist).^2)/(inSigma^2));
% outKernel = outKernel - diag(diag(outKernel));
end

