%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%the purpose of the following codes is to test the pcn algorithm 
%after you test the algorithm, you can comments these codes, and at the
%same time umcomments the function's name and its 'end' statement
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% a = [[0,0,0];[0,1,0];[1,0,0];[1,1,1]];
% b = [[0,0,0];[0,1,1];[1,0,1];[1,1,0]];
% 
% inputs = a(:, 1:2);
% targets = a(:,3)
% eta = 0.25;
% nIterations = 10;
%		p = self.pcn(a[:,0:2],a[:,2:])
%		p.pcntrain(a[:,0:2],a[:,2:],0.25,10)
%		p.confmat(a[:,0:2],a[:,2:])

%		q = self.pcn(a[:,0:2],b[:,2:])
%		q.pcntrain(a[:,0:2],b[:,2:],0.25,10)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function: find k pieces of center points for inputs data
%               
% Inputs Parameters: 
%             inputs: inputs' data,that is, which will be trained
%             targets: 
%             eta:    learning rate
%             nIterations: the number of interation 
% Outputs Parameters:
%             weights matrix
%
%The codes is written according to the Python code pcn.py and the
%algorithm written on the page of 23
%
%-----by Xueli Huang  9/20/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function weights = pcn(inputs, targets, eta, nIterations)  
nIterations
%Initialisation 
[nData, nIn] = size(inputs);
nOut = size(targets,2);


% set all of the weights to samll random numbers
weights = rand(nOut, nIn)*0.1 - 0.05;

%Training
y = zeros(size(inputs,1), 1);
for j= 1: nIterations
    for i = 1:size(inputs, 1)
        y(i) = sum( weights .* inputs(i,:) );
        if y(i) > 1.5
            y(i) = 2;
        else
            y(i) = 1;
        end
       % y(i)
        weights = weights + eta*(targets(i)-y(i))*inputs(i, :);
        
    end    
end
%y'

%y
%figure, hold on;
%help_ind = find(targets == 1);
%plot( inputs(:,1), inputs(:,2), '.g', 'markersize', 40 );
%plot( inputs(help_ind,1), inputs(help_ind,2), '.r', 'markersize', 40 );

%plot( [weights(3)/weights(1),0], [0,weights(3)/weights(2)], 'LineWidth',5 );
   
    
    
    
    
    
  