//// Testing generalization perfroamnce of trained perceptron
  exec perceptronTrain.sce;
 
 function r=randomFlipMatrix(n);
 // returns matrix with components 1 at n random positions of each column
  r=zeros(156,26);
  for i=1:26
    x=grand(1,'prm',(1:156)');
    r(x(1:n),i)=1;
  end
 endfunction
 
 letterMatrix=rIn; 
 for nflip = 1:80;    
    dist1=[]; dist2=[]; 
    for trial=1:10; 
       rIn=abs(letterMatrix-randomFlipMatrix(nflip)); 
       // Threshold output function
       rOut1=zeros(26,26); rOut1((wOut*rIn)>0)=1;
       nerror=0; 
       for j=1:26; nerror=nerror+(sum(rDes(:,j)~=rOut1(:,j))>0); end 
       dist1=[dist1,nerror];
       // Max output function
       [v,k]=max(wOut*rIn,'r');
       rOut2=zeros(26,26); for j=1:26; rOut2(k(j),j)=1; end
       dist2=[dist2,0.5*sum(sum(rDes~=rOut2))];
    end
    meanDist1(nflip)=mean(dist1); stdDist1(nflip)=st_deviation(dist1);
    meanDist2(nflip)=mean(dist2); stdDist2(nflip)=st_deviation(dist2);
 end
 figure;
 plot((1:80)/156,meanDist1,':')
 errbar((1:80)/156,meanDist1,stdDist1,stdDist1)
 plot((1:80)/156,meanDist2,'r')
 errbar((1:80)/156,meanDist2,stdDist2,stdDist2)
 xlabel('Fraction of flipped bits')
 ylabel('Average number of wrong letters')
