-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeK.m
More file actions
30 lines (28 loc) · 871 Bytes
/
Copy pathcomputeK.m
File metadata and controls
30 lines (28 loc) · 871 Bytes
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 = computeK(kernel_type, X, Z, param)
% function K = computeK(kernel_type, X, Z)
% computes a matrix K such that Kij=g(x,z);
% for three different function linear, rbf or polynomial.
%
% Input:
% kernel_type: either 'linear','polynomial','rbf'
% X: n input vectors of dimension d (dxn);
% Z: m input vectors of dimension d (dxm);
% param: kernel parameter (inverse kernel width gamma in case of RBF, degree in case of polynomial)
%
% OUTPUT:
% K : nxm kernel matrix
%
%
if nargin<2,
Z=X;
end;
%%YOUR CODE HERE
[d , n] = size(X);
[d , m] = size(Z);
if (strcmp(kernel_type , 'linear') == 1)
K = X' * Z;
elseif (strcmp(kernel_type , 'rbf') == 1)
K = exp(-param .* (repmat(sum(X .* X ,1)', 1 , m) - 2 .* X' * Z + repmat(sum(Z .* Z,1), n , 1)));
elseif (strcmp(kernel_type , 'poly') == 1)
K = (X' * Z + 1) .^ param;
end;