close all; clear all; %PCA example -------------------------------------------------- x = [1,2; 3,3; 3,5; 5,4; 5,6; 6,5; 8,7; 9,8] N = size(x,1); D = size(x,2); subplot(121),scatter(x(:,1),x(:,2),'filled'); axis([0 10 0 10]) set(gca,'XTick',0:10); set(gca,'YTick',0:10); grid on; title('Original space') mu = mean(x); s = cov(x); [u,l,d] = svd (s); y = (x-repmat(mu,N,1)) * u; % plotting First principal component hold on; F = 2; h = line(mu(1) + [0 u(1,1)]* F , mu(2) + [0 u(1,2)] * F); set(h,'Color', 'r'); % plotting Second principal component h = line(mu(1) + [0 u(2,1)]* F , mu(2) + [0 u(2,2)] * F); set(h,'Color', 'r'); hold off; subplot(122),scatter(y(:,1),y(:,2),'filled') set(gca,'XTick',-6:6); set(gca,'YTick',-2:0.5:2); grid on; title('Projected space') l = diag (l); l(1)/sum(l) l(2)/sum(l) %LDA example ------------------------------------------------ x = [4,1; 2,4; 2,3; 3,6; 4,4; 9,10; 6,8; 9,5; 8,7; 10,8;] c = [1,1,1,1,1,2,2,2,2,2]'; N = size(x,1); [xp, W,lambda] = LDA(x,c); mu = mean(x); figure; subplot(121),gscatter(x(:,1),x(:,2),c); axis([0 11 0 11]) set(gca,'XTick',0:11); set(gca,'YTick',0:11); grid on; legend off; title('Original space') %plotting Fisher projection hold on; F = 5; h = line(mu(1) + [0 W(1,1)]* F , mu(2) + [0 W(2,1)] * F); set(h,'Color', 'r'); subplot(122),gscatter(xp(:,1),zeros(N,1),c); axis([0 11 -1 1]) set(gca,'XTick',0:11); set(gca,'YTick',-1:1); grid on; legend off; title('Projected space')