gusucode.com > 《模式识别与智能计算》matlab源代码 > 《模式识别与智能计算》matlab源代码/《模式识别与智能计算》部分函数的源程序/书程序/第5章/myKernel.m

    function K=myKernel(x,y,opts) 
if (~exist('opts','var'))
   opts = [];
else
   if ~isstruct(opts)
       error('parameter error!');
   end
end
N=size(x,1);
if isempty(y)
    K=zeros(N,N);
else
    M=size(y,1);
    if size(x,2)~=size(y,2)
        error('X and Y should have the same row dimensionality!');
    end;
    K=zeros(N,M);
end;
if ~isfield(opts,'KernelType')
    opts.KernelType = 'Gaussian';   
end
switch lower(opts.KernelType)
    case {lower('Gaussian')}        
        if ~isfield(opts,'gamma')
            opts.gamma = 0.5;
        end
    case {lower('RBF')}        
        if ~isfield(opts,'gamma')
            opts.gamma = 10;
        end
    case {lower('Polynomial')}      
        if ~isfield(opts,'d')
            opts.d = 1;
        end
    case {lower('PolyPlus')}     
        if ~isfield(opts,'d')
            opts.d = 1;
        end
    case {lower('tanh')}          
        if ~isfield(opts,'g')||~isfield(opts,'c')
            opts.g = 1;
            opts.c = 1;
        end
    otherwise
        error('KernelType does not exist!');
end
switch lower(opts.KernelType)
    case {lower('Gaussian')}      
        if isempty(y)
            for i=1:N
                for j=i:N
                    dist = sum(((x(i,:) - x(j,:)).^2));
                    temp=exp(-opts.gamma*dist);
                    K(i,j)=temp;
                    if i~=j
                        K(j,i)=temp;
                    end;
                end
            end
        else
            for i=1:N
                for j=1:M
                    dist = sum(((x(i,:) - y(j,:)).^2));
                    K(i,j)=exp(-opts.gamma*dist);
                end
            end
        end      
    case {lower('Polynomial')}    
        if isempty(y)
            for i=1:N
                for j=i:N                   
                    temp=(x(i,:)*x(j,:)')^opts.d;
                    K(i,j)=temp;
                    if i~=j
                        K(j,i)=temp;
                    end
                end
            end
        else
            for i=1:N
                for j=1:M                                      
                    K(i,j)=(x(i,:)*y(j,:)')^opts.d;
                end
            end 
        end      
    case {lower('PolyPlus')}    
        if isempty(y)
            for i=1:N
                for j=i:N                   
                    temp=(x(i,:)*x(j,:)'+1)^opts.d;
                    K(i,j)=temp;
                    if i~=j
                        K(j,i)=temp;
                    end;
                end
            end
        else
            for i=1:N
                for j=1:M                                      
                    K(i,j)=(x(i,:)*y(j,:)'+1)^opts.d;
                end
            end
        end
     case {lower('RBF')}      
        if isempty(y)
            for i=1:N
                for j=i:N
                    dist = sum(((x(i,:) - x(j,:)).^2));
                    temp=exp(-0.5*dist/opts.gamma^2);
                    K(i,j)=temp;
                    if i~=j
                        K(j,i)=temp;
                    end;
                end
            end
        else
            for i=1:N
                for j=1:M
                    dist = sum(((x(i,:) - y(j,:)).^2));
                    K(i,j)=exp(-0.5*dist/opts.gamma^2);
                end
            end
        end      
    case {lower('tanh')}
         if isempty(y)
            for i=1:N
                for j=i:N                   
                    temp=opts.g*x(i,:)*x(j,:)'+opts.c;
                    K(i,j)=temp;
                    if i~=j
                        K(j,i)=temp;
                    end
                end
            end
        else
            for i=1:N
                for j=1:M                                      
                    K(i,j)=opts.g*x(i,:)*y(j,:)'+opts.c;
                end
            end 
        end      
    otherwise
        error('KernelType does not exist!');
end