forked from pzczxs/MTLSSVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kerfun.m
30 lines (30 loc) · 886 Bytes
/
Kerfun.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
function [K] = Kerfun(kernel, X, Z, p1, p2)
%
% K = Kerfun(kernel, X, Z, p1, p2)
% kernel: the type of kernel function
% X: m*p matrix
% Z: n*p matrix
% p1, p2: corresponding parameters in kernel function
% author£ºXU Shuo ([email protected])¡£
%
if size(X, 2) ~= size(Z, 2)
K = [];
display('The second dimensions for X and Z must be agree.');
return;
end
switch lower(kernel)
case 'linear'
K = X*Z';
case 'poly'
K = (X*Z' + p1).^p2;
case 'rbf'
K = exp(-p1*(repmat(dot(X, X, 2), 1, size(Z, 1)) + ...
repmat(dot(Z, Z, 2)', size(X, 1), 1) - 2*X*Z'));
case 'erbf'
K = exp(-sqrt(repmat(dot(X, X, 2), 1, size(Z, 1)) + ...
repmat(dot(Z, Z, 2)', size(X, 1), 1) - 2*X*Z') / (2*p1^2)) + p2;
case 'sigmoid'
K = tanh(p1*X*Z'/size(X, 2) + p2);
otherwise
K = X*Z' + p1 + p2;
end