简体   繁体   中英

Implementation of Gaussian RBF in Matlab

I'm starting with Machine Learning and needed help with implementing Gaussian RBF in Matlab.

在此处输入图片说明

I know what it is doing, but not sure how to implement this in matlab

function K = rbf(coord,sig)

%function K = rbf(coord,sig)
%
% Computes an rbf kernel matrix from the input coordinates
%
%INPUTS
% coord =  a matrix containing all samples as rows
% sig = sigma, the kernel width; squared distances are divided by
%       squared sig in the exponent
%
%OUTPUTS
% K = the rbf kernel matrix ( = exp(-1/(2*sigma^2)*(coord*coord')^2) )
%

n=size(coord,1);
K=coord*coord'/sig^2;
d=diag(K);
K=K-ones(n,1)*d'/2;
K=K-d*ones(1,n)/2;
K=exp(K);

%% Previous version:
%%
% n = size(coord,1);
% for i=1:n
%     K(i,i)=1;
%     for j=1:i-1
%         K(i,j)=exp(-norm(coord(i,:)-coord(j,:))^2/sig^2);% Should be
%         % 2*sig^2!
%         K(j,i)=K(i,j);
%     end
% end

( Source )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM