
%Only two by two

%CN-MCI



load Fl15

%control-dcl
cxyz=[];




g2=[repmat(1,1,12) repmat(2,1,6)];


nrep=18;

for i=1:nrep
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    
end

xmi=cxyz;



   %%%%%%%%%%%%%%%
  %leave-one out 
  
 e=[];
  for ni=1:20 
  

errores=0;
     
for i=1:18

 indi=ones(18,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:18,i);

  tik=zeros(17,max(size(xmi)));
  
for k=s
    
    indi2=ones(18,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:12,[i k]);
  segun=setdiff(13:18,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 
%
 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g2(indi));

c1 = svmclassify(svmStruct,xmi(i,feat(1:ni)));
 

errores=errores + double(c1~=g2(i));


if(c1~=g2(i))

[i,c1]

end
end

    
e=[e errores]; %
  
  end
  
  e =

     7     7     7     7     9     9     9     9     7     7     9     4     7     5     5     5     5     6     6     7
     
    
     
     para ni=12
     ans =

     8     2


ans =

    15     1


ans =

    17     1


ans =

    18     1
     
     
 %eleccion d enumero comoenetes
 %No. components
 
 erl=zeros(18,20);
 
 e=[];
  for ni=1:20 %
  

errores=0;
     
for i=1:18

 indi=ones(18,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:18,i);

  tik=zeros(17,max(size(xmi)));
  
for k=s
    
    indi2=ones(18,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:12,[i k]);
  segun=setdiff(13:18,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 

 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g2(indi));
  
  
  

      errores=0;
grupo=g2(indi);
for j=1:17
    
    indi2=ones(17,1);
    
    indi2(j)=0;
   indi2=logical(indi2);
   c1 = svmclassify(svmStruct,xmi(j,feat(1:ni)));

errores=errores + double(c1~=grupo(j));

end

erl(i,ni)=errores;


end

    

  
  end
 
  figure
plot(1:20,sum(erl,1)) % mínimo en  12 (4 errors) 

sum(erl,1)

ans =

    94    94    91    67    94    94    59    66    18    18    20    15    18    16    16    16    16    17    17    18

 
  %%%%%%%%%%%
  
  %CN- AD
  
  g4=[repmat(1,1,12) repmat(3,1,10)];
 
 cxyz=[];
 
 do=[1:12 19:28];
 
for i=do
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    i
end

xmi=cxyz;

 %leave-one out 
  
 e=[];
  for ni=1:24 %
  

errores=0;
     
for i=1:22

 indi=ones(22,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:22,i);

  tik=zeros(21,max(size(xmi)));
  
for k=s
    
    indi2=ones(22,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:12,[i k]);
  segun=setdiff(13:22,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 

 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g4(indi));

c1 = svmclassify(svmStruct,xmi(i,feat(1:ni)));
 

errores=errores + double(c1~=g4(i));


if(c1~=g4(i))

[i,c1]

end
end

    
e=[e errores]; %
  
  end

e =

    13    14     9     8     8     8     6     7     5     6     4     6     6     8     9     9     1     1     1     0     0     0     1     1

   % menor 0 pero con 20 características, cuando hay 12+10=22 individuos
    
    
    
    %seleccion de nº de caracteristicas
    %No. components
    
  erl=zeros(22,24);
    
    for ni=1:24 
  

errores=0;
     
for i=1:22

 indi=ones(22,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:22,i);

  tik=zeros(21,max(size(xmi)));
  
for k=s
    
    indi2=ones(22,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:12,[i k]);
  segun=setdiff(13:22,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 

 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g4(indi));


      errores=0;
grupo=g4(indi);
for j=1:21
    
    indi2=ones(21,1);
    
    indi2(j)=0;
   indi2=logical(indi2);
   c1 = svmclassify(svmStruct,xmi(j,feat(1:ni)));

errores=errores + double(c1~=g4(j));

end

erl(i,ni)=errores;


end

  
  
  end


     figure
plot(1:24,sum(erl,1)) % mínimo en  20 (0 errors) 

sum(erl,1)


   199   131   147    70    53    50    38    23     5     6     4     6     6     8     9     9     1     1     1     0     0     0     1     1


   %%%%%%%%%%%%
   % MCI-AD
   
    g4=[repmat(2,1,6) repmat(3,1,10)];
 
 cxyz=[];
 
 do=[13:28];
 
for i=do
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    
end

xmi=cxyz;

 %leave-one out 
  
 e=[];
  for ni=1:12
  

errores=0;
     
for i=1:16

 indi=ones(16,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:16,i);

  tik=zeros(15,max(size(xmi)));
  
for k=s
    
    indi2=ones(16,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:6,[i k]);
  segun=setdiff(7:16,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 

 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g4(indi));

c1 = svmclassify(svmStruct,xmi(i,feat(1:ni)));
 

errores=errores + double(c1~=g4(i));


if(c1~=g4(i))

[i+12,c1]

end
end

    
e=[e errores]; %
  
  end

    e =

     8     6     7     4     4     5     6     5     6     6     7     7

    
     %ni=8 errors:
     
     ans =

    13     3


ans =

    14     3


ans =

    15     3


ans =

    17     3


ans =

    26     2
     
     
  %No. components  
    %seleccion de nº de caracteristicas
    
  erl=zeros(16,12);
    
    for ni=1:12 %
  

errores=0;
     
for i=1:16

 indi=ones(16,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:16,i);

  tik=zeros(15,max(size(xmi)));
  
for k=s
    
    indi2=ones(16,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:6,[i k]);
  segun=setdiff(7:16,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 

 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g4(indi));


      errores=0;
grupo=g4(indi);
for j=1:15
    
    indi2=ones(15,1);
    
    indi2(j)=0;
   indi2=logical(indi2);
   c1 = svmclassify(svmStruct,xmi(j,feat(1:ni)));

errores=errores + double(c1~=g4(j));

end

erl(i,ni)=errores;


end

  
  
  end


     figure
plot(1:12,sum(erl,1)) % mínimo en 8 (5 errors) 

sum(erl,1)

   
     86    78    72    36    31    31    29     5     6     6     7     7
   
  
     
     %%%%%%%%%%%%%%%%%%%%
     %%%%%%%%%%%%%%
     
     %Test
     %Kernel RBF

     %CN-MCI
     
      %leave-one out 
  
  sigma=2^-8; %changing
 e=[];
  for ni=1:20 
  

errores=0;
     
for i=1:18

 indi=ones(18,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:18,i);

  tik=zeros(17,max(size(xmi)));
  
for k=s
    
    indi2=ones(18,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:12,[i k]);
  segun=setdiff(13:18,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 
 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g2(indi),'KERNEL_FUNCTION','rbf','RBF_SIGMA',sigma);

c1 = svmclassify(svmStruct,xmi(i,feat(1:ni)));
 

errores=errores + double(c1~=g2(i));


if(c1~=g2(i))

[i,c1]

end
end

    
e=[e errores]; %
  
  end

%sigma:    errors
%2^-8:   todos 6, all 6
%2^-5:  8 resto 6, 8 rest 6
%2^-10: todos 6, all 6
%2^-6: todos 6, all 6
%2^-4: 7 resto 6, 7 rest 6
%2^-2: 7 4 5 resto 6
%  2^0: cinco 7's resto 6
%  2^2:   5     6     6     5     5     5     5     5     5     5     5     6     6     6     7     6     5     5     5     5
%  2^-1:  7     4     6     5     6     6     6     6     6     6     6     6     6     6     6     6     6     6     6     6
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%
     
     %CN-AD
     
 g4=[repmat(1,1,12) repmat(3,1,10)];
 
 cxyz=[];
 
 do=[1:12 19:28];
 
for i=do
    
    r=find(Fl(i).x); %son los distintos de cero
    s=find(Fl(i).y);
    t=find(Fl(i).z);
    cxyz=[cxyz; Fl(i).x(r)' Fl(i).y(s)' Fl(i).z(t)'];
    i;
end

xmi=cxyz;

 %leave-one out 
  
 sigma=2^-8;
 
 e=[];
  for ni=1:24 
  

errores=0;
     
for i=1:22

 indi=ones(22,1);
 indi(i)=0;
 indi=logical(indi);


  
  s=setdiff(1:22,i);

  tik=zeros(21,max(size(xmi)));
  
for k=s
    
    indi2=ones(22,1);
    indi2(i)=0;
    indi2(k)=0;
   indi2=logical(indi2);
   
    primer=setdiff(1:12,[i k]);
  segun=setdiff(13:22,[i k]);
    
   
   [h,p,ci,stats]=ttest2(xmi(primer,:),xmi(segun,:));

tik(k,:)=stats.tstat;
   
end
 
 [Y,I] = min(abs(tik),[],1); 
  [sy,feat] = sort(Y,'descend');
  
  

datos=xmi(indi,feat(1:ni));
 
  svmStruct = svmtrain(datos,g4(indi),'KERNEL_FUNCTION','rbf','RBF_SIGMA',sigma);

c1 = svmclassify(svmStruct,xmi(i,feat(1:ni)));
 

errores=errores + double(c1~=g4(i));


if(c1~=g4(i))

[i,c1]

end
end

    
e=[e errores]; %
  
  end


 % sigma=        errores
%2^-8:   todos 10
%
%2^-6: 12 resto 10
%
%2^-2: 15 10 11 resto 10
 % 2^0:  15     9     8     8    11    10    10     8     8     8    10    10    10    10    10    10    10    10    10    10    10    10    10    10
  