clear all;
close all;
clc;

  load datasource_colon; 
  %%% Load the toumor data =genes*samples
 
  traindata0=colon_train;
  testdata0=colon_test;
  trainlabels0=colon_tnCL;
  testlabels0=colon_ttCL;
   



traindata0=mapstd(traindata0', 0, 1)' ;
testdata0=mapstd(testdata0', 0, 1)' ;

 species=[trainlabels0, testlabels0]';
 meas=[traindata0, testdata0]';



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 10 fold cross

 indices = crossvalind('Kfold',species,10);
 cp = classperf(species);
 for i = 1:10
      test = (indices == i); train = ~test;
         
trainlabels=species(train, :)';
testlabels=species(test, :)';
traindata=meas(train,:)';
testdata=meas(test,:)';

% % % ****************************%%%%%%%%%%%%%%%%USING PCA
classids = unique(trainlabels);
NumClass = length(classids);

traindata1=[];
trainlabels1=[];
for k=1:NumClass
  q=size(traindata(:,find(trainlabels==k)),2);
  
 Numeigv=3;
[eigvector{1,k}, eigvalue] = svd(traindata(:,find(trainlabels==k)), 0);
 traindata1=[traindata1,eigvector{1,k}(:,1:Numeigv)]; 
Tlabels(1:Numeigv,k)=k;
trainlabels1=[trainlabels1; Tlabels(1:Numeigv,k)];
end

 traindata=traindata1;
 trainlabels=trainlabels1;
 
 %%%%%%%%%%%%%%%%%%%%%%%%%% not using PCA
 
  lambda=0.1;
  ids=[];
   for j = 1:size(testdata, 2)
        y = testdata(:,j);
        [id, s] = MSRC(traindata, trainlabels, y, lambda);
        ids(j) = id;
%         x(:,j)=s;
   end
  class=ids';
  classperf(cp,class,test);
 end

classrate=1- cp.ErrorRate
   

    
    
