-
Notifications
You must be signed in to change notification settings - Fork 1
/
kcenter.m
39 lines (34 loc) · 798 Bytes
/
kcenter.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
function K = kcenter(K)
% kcenter - center a kernel matrix
%
% Synopsis:
% Kc = kcenter(K);
%
% Arguments:
% K: Kernel matrix
%
% Return:
% Kc: centered kernel matrix
%
% Code-Reference:
% Shawe-Taylor, Cristianini, 'Kernel Methods for Pattern Analysis',
% Cambridge University Press, 2004, http://www.kernel-methods.net
%
% Authors: K. Rieck, M. Kloft
%
n = size(K,2);
if ismatrix(K)
D = sum(K) / n;
E = sum(D) / n;
J = ones(n,1) * D;
K = K - J - J' + E * ones(n, n);
K = 0.5 * (K + K');
elseif ndims(K)==3
for i=1:size(K,3)
D = sum(K(:,:,i)) / n;
E = sum(D) / n;
J = ones(n,1) * D;
K(:,:,i) = K(:,:,i) - J - J' + E * ones(n, n);
K(:,:,i) = 0.5 * (K(:,:,i) + K(:,:,i)') + 1e-12*eye(n);
end
end