PCA - Principle Components Analyses
close all;
clear all;
clc;
% VARIABLES
vars = 2;
samples = 20;
nComponents = 2;
% 1) Normalize data
data=rand(vars,samples);
% 2) Remove mean
MeanMatrix = repmat(mean(data,2),1,size(data,2));
data=data-MeanMatrix;
% 3) Calculate covariance matrix
CovMatrix = cov(data');
% 3) Calculate eigenvectors and eigenvalues of the covariance matrix
[FeatureMatrix, EvalueMatrix] = eig(CovMatrix);
Evalues = diag(EvalueMatrix)
% 4) Order by eigenvalue
Evalues = Evalues(end:-1:1)
FeatureMatrix = FeatureMatrix(:,end:-1:1);
FeatureMatrix=FeatureMatrix';
% 5) Take first n E-vectors
FeatureMatrix = FeatureMatrix(1:nComponents,:);
% 6) Project data
ProjectedData = FeatureMatrix * data;
% 7) Reconstruction
ReconstructedData = (FeatureMatrix' * ProjectedData) + MeanMatrix;
% plot
subplot(2,2,1);
plot(data(1,:),data(2,:),'.');
subplot(2,2,2);
plot(ProjectedData(1,:),ProjectedData(2,:),'.');
subplot(2,2,3);
plot(ReconstructedData(1,:),ReconstructedData(2,:),'.');