-
Notifications
You must be signed in to change notification settings - Fork 11
/
Eigenface_f.m
60 lines (43 loc) · 1.6 KB
/
Eigenface_f.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function [disc_set,disc_value,Mean_Image]=Eigenface_f(Train_SET,Eigen_NUM)
% The magnitude of eigenvalues of this function is corrected right
% Centralized PCA
[NN,Train_NUM]=size(Train_SET);
% for large sample size case
if NN<=Train_NUM
Mean_Image=mean(Train_SET,2);
Train_SET=Train_SET-Mean_Image*ones(1,Train_NUM);
R=Train_SET*Train_SET'/(Train_NUM-1);
%Find the max eigenvalue and its corresponding eigenvector
[V,S]=Find_K_Max_Eigen(R,Eigen_NUM);
disc_value=S;
disc_set=V;
% for small sample size case, singular value decomposition
else
Mean_Image=mean(Train_SET,2);
Train_SET=Train_SET-Mean_Image*ones(1,Train_NUM);
R=Train_SET'*Train_SET/(Train_NUM-1);
[V,S]=Find_K_Max_Eigen(R,Eigen_NUM);
disc_value=S;
disc_set=zeros(NN,Eigen_NUM);
Train_SET=Train_SET/sqrt(Train_NUM-1);
for k=1:Eigen_NUM
disc_set(:,k)=(1/sqrt(disc_value(k)))*Train_SET*V(:,k);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FInd the max eigenvalue and its corresponding eigenvector, sort them decreasingly.
function [Eigen_Vector,Eigen_Value]=Find_K_Max_Eigen(Matrix,Eigen_NUM)
[NN,NN]=size(Matrix);
%Note this is equivalent to; [V,S]=eig(St,SL); also equivalent to [V,S]=eig(Sn,St); %
[V,S]=eig(Matrix);
S=diag(S);
[S,index]=sort(S);
Eigen_Vector=zeros(NN,Eigen_NUM);
Eigen_Value=zeros(1,Eigen_NUM);
p=NN;
for t=1:Eigen_NUM
Eigen_Vector(:,t)=V(:,index(p));
Eigen_Value(t)=S(p);
p=p-1;
end