forked from cnmusco/recursive-nystrom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussianKernel.m
33 lines (32 loc) · 1.06 KB
/
gaussianKernel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function Ksub = gaussianKernel(X,rowInd,colInd,gamma)
%% Guassian kernel generator
% Outputs a submatrix of the Gaussian kernel with variance paramater
% gamma for the data rows of X.
%
% usage :
%
% input:
%
% * X : A matrix with n rows (data points) and d columns (features)
%
% * rowInd, colInd : Lists of indices between 1 and n.
%
% NOTE: colInd can be an empty list, in which case the **diagonal**
% entries of the kernel will be output for the indices in rowInd.
%
% * gamma : kernel variance parameter
%
% output:
%
% * Ksub : Let K(i,j) = e^-(gamma*||X(i,:)-X(j,:)||^2). Then Ksub =
% K(rowInd,colInd). Or if colInd = [] then Ksub = diag(K)(rowInd).
if(isempty(colInd))
Ksub = ones(length(rowInd),1);
else
nsqRows = sum(X(rowInd,:).^2,2);
nsqCols = sum(X(colInd,:).^2,2);
Ksub = bsxfun(@minus,nsqRows,X(rowInd,:)*(2*X(colInd,:))');
Ksub = bsxfun(@plus,nsqCols',Ksub);
Ksub = exp(-gamma*Ksub);
end
end