%seloalgo.m 
function [PD,res]=seloalgo(M,sigma) 
%Tuning the sigma value for KLDA to %maximize the objective function J=trace(E'*SB*E)/ trace(E'*SW*E) %(Computed using kernel function)
for i=1:1:12 
    for j=1:1:12 
        G(i,j)=gaussiankernel(M(:,i),M(:,j),sigma);
    end
end
%Between-class scatter matrix 
C1=mean(G(:,1:1:4)')'; 
C2=mean(G(:,5:1:8)')'; 
C3=mean(G(:,9:1:12)')';
C=(C1+C2+C3)*(1/3); 
SB=(C1-C)*(C1-C)'+(C2-C)*(C2-C)'+(C3-C)*(C3-C)'; 
SB=4*SB/(4-3); 
SW=(G(:,1:1:4)-repmat(C1,1,4))*(G(:,1:1:4)-repmat (C1,1,4))'+(G(:,5:1:8)-repmat(C2,1,4))*(G(:,5:1:8)-repmat (C2,1,4))'+(G(:,9:1:12)-repmat(C3,1,4))*(G(:,9:1:12)-repmat (C3,1,4))'; 
SW=SW/(12-3); [E,D]=eig(pinv(SW)*SB); 
E=real(E(:,1)); 
res=abs(trace(E'*SW*E)/trace(E'*SB*E)); res=real(res); 
PD=E'*G;