%Maximizing Gaussianity after applying K-LDA 
x=-1:0.1:1; 
y=-1:0.1:1; 
for i=1:1:length(x) 
for j=1:1:length(y) 
    z(i,j)=x(i)^2+y(j)^2; 
end
end
[c,h]=contour(x,y,z);
[x1,y1]=find(0.2<z<0.6); 
[x2,y2]=find(z>0.8); 
%kernel data 
data1=(([x1 y1]/21)*2-1)'; 
data2=(([x2 y2]/21)*2-1)';
figure 
plot(data1(1,:),data1(2,:),'r*')
hold on 
plot(data2(1,:),data2(2,:),'b*')
X=[data1 data2]; 
sigma=0.2; 
split=[1 length(data1) length(data2)] 
for i=1:1:size(X,2) 
    for j=1:1:size(X,2) 
        temp(i,j)=gaussiankernel(X(:,i),X(:,j),sigma); 
    end
end
X=temp; SB=0; MEANDATA=[]; 
for i=1:1:length(split)-1 
    DATA=X(:,split(i):1:split(i)+split(i+1)); 
    n=size(DATA,2); M=mean(DATA');
    temp1=(DATA-repmat(M',1,size(DATA,2)));
    temp2=temp1*temp1';
    SB=SB+n*temp2; 
    MEANDATA=[MEANDATA;M];
end
size(MEANDATA)
M=mean(MEANDATA);
temp3=(MEANDATA'-repmat(M',1,size(MEANDATA',2)));
SW=temp3*temp3';
SW=real(SW);
[E,V]=eig(pinv(SW)*SB);
V=diag(real(V))/max(diag(real(V)));
Esel=E(:,1:1:2);
L=size(Esel,2);
Y=real(Esel)'*X;
data1=maxgauss(real(Y(:,1:1:split(2))),[2 2]);
data2=maxgauss(real(Y(:,split(2)+1:1:split(2)+split (3))),[-2 -2]);
figure 
plot(data1(1,:),data1(2,:),'r*') 
hold on 
plot(data2(1,:),data2(2,:),'b*')
figure 
plot(Y(1,1:1:split(2)),Y(2,1:1:split(2)),'r*'); 
hold on 
plot(Y(1,split(2)+1:1:split(2)+split(3)),Y(2,split(2) +1:1:split(2)+split(3)),'b*');

