-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKernelMatrix.m
42 lines (38 loc) · 1.37 KB
/
KernelMatrix.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
34
35
36
37
38
39
40
41
42
function omega = KernelMatrix(Xtrain,kernel_type, kernel_pars,Xt)
% Construct the positive (semi-) definite and symmetric kernel matrix
%
% >> Omega = kernel_matrix(X, kernel_fct, sig2)
%
% This matrix should be positive definite if the kernel function
% satisfies the Mercer condition. Construct the kernel values for
% all test data points in the rows of Xt, relative to the points of X.
%
% >> Omega_Xt = kernel_matrix(X, kernel_fct, sig2, Xt)
%
%
% Full syntax
%
% >> Omega = kernel_matrix(X, kernel_fct, sig2)
% >> Omega = kernel_matrix(X, kernel_fct, sig2, Xt)
%
% Outputs
% Omega : N x N (N x Nt) kernel matrix
% Inputs
% X : N x d matrix with the inputs of the training data
% kernel : Kernel type (by default 'RBF_kernel')
% sig2 : Kernel parameter (bandwidth in the case of the 'RBF_kernel')
% Xt(*) : Nt x d matrix with the inputs of the test data
% Copyright (c) 2012, Siamak Mehrkanooon
nb_data = size(Xtrain,1);
if strcmp(kernel_type,'RBF_kernel'),
if nargin<4,
XXh = sum(Xtrain.^2,2)*ones(1,nb_data);
omega = XXh+XXh'-2*(Xtrain*Xtrain');
omega = exp(-omega./(kernel_pars(1)));
else
XXh1 = sum(Xtrain.^2,2)*ones(1,size(Xt,1));
XXh2 = sum(Xt.^2,2)*ones(1,nb_data);
omega = XXh1+XXh2' - 2*Xtrain*Xt';
omega = exp(-omega./(kernel_pars(1)));
end
end