%kernellda.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Kernel Linear Discriminant Analysis (Using Gaussian kernel) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
m1=rand(3,1)*15-2;
m2=rand(3,1)*5+103;
m3=rand(3,1)*1+1; 
N1=100; N2=100; N3=100; 
temp1=rand(length(m1),1)*5-1;
C1=temp1*temp1';
temp2=rand(length(m2),1)*5+3;
C2=temp2*temp2';
temp3=rand(length(m3),1)*5+1;
C3=temp3*temp3';
[X1,C1est]=genrandn(m1,C1,N1);
[X2,C2est]=genrandn(m2,C2,N2);
[X3,C3est]=genrandn(m3,C3,N3); 
X=[X1 X2 X3]; 
X=normalizedata(X); 
figure(5) 
subplot(2,3,1) 
scatter3(X(1,:),X(2,:),X(3,:),'r*') 
title('original data in scatter plot') 
subplot(2,3,2) 
imagesc(X) 
title('original data in image format') 
visualizedata(X,2,3,3,5) 
title('Distance matrix of the original data') 
[E1,Y1,L1]=dimredklda(X,[100 100 100],0.1); 
Y1=normalizedata(Y1); 
subplot(2,3,4) 
scatter(Y1(1,:),zeros(1,length(Y1)),'r*')
title('Projected data using KLDA (normalized threshold=0.1)') 
subplot(2,3,5) 
Y1=real(Y1); 
imagesc(Y1) 
title('Projected data using KLDA') 
visualizedata(Y1,2,3,6,5) 
title('Distance matrix of the dimensionality reduced data using KLDA (normalized threshold=0.1)')

%With real world image data 
load IMAGEDATA 
figure(6) 
subplot(2,2,1) 
X=normalizedata(X); 
imagesc(X) 
title('Original image data') 
visualizedata(X,2,2,2,6) 
title('Distance matrix of the original image data') 
[E1,Y1,L1]=dimredklda(X,ones(1,10)*40,0.1); 
Y1=real(Y1); 
subplot(2,2,3) 
Y1=normalizedata(Y1); 
imagesc(Y1);
title('Data after dimensionality reduction after KLDA') 
visualizedata(real(Y1),2,2,4,6) 
title('Distance matrix of the dimensionality reduced data using ... KLDA (normalized threshold=0.1)')
figure(7) 
visualizedata(Y1(:,1:1:120),2,2,4,7) 
title('Distance matrix of the dimensionality reduced data using KLDA')