function out=select_genes(X,target,numClass,numGenes,method)

%Reduce the number of genes which are used for variable selection.
%This is done by using a test Statistic.There are two test statistics 
%"Dudoit" and "Nguyen".

[n,p]=size(X);

switch method
    case 'Dudoit'
        Num=zeros(1,p);
        Den=zeros(1,p);
                
         for i=1:numClass,
            IndexClass=find(target==i-1);
            MeanClass=mean(X(IndexClass,:),1);
            LengthClass=length(IndexClass);
            Num=Num+MeanClass.^2*LengthClass;
            Den=Den+var(X(IndexClass,:),1)*LengthClass;
        end;
        Statistic=Num./Den;
        
    case 'Nguyen'
        
        for i=1:numClass,
            IndexClass=find(target==i-1);
            N(i)=length(IndexClass);
            Mean(i,:)=sum(X(IndexClass,:),1)/N(i);
            CtrX=X(IndexClass,:)-repmat(Mean(i,:),N(i),1);
            SumSquare(i,:)=sum(CtrX.^2,1);
        end;
        Statistic=(Mean(1,:)-Mean(2,:))./sqrt(SumSquare(1,:)/(N(1)*(N(1)-1))+SumSquare(2,:)/(N(2)*(N(2)-1)));
        
end

%Conclusions.
[out1,out2]=sort(Statistic,'descend');
index=sort(out2(1:numGenes),'ascend');
out.GENES=X(:,index);
out.Index=index;
