---
# System prepended metadata

title: MATLAB

---

# MATLAB

## Week3

# PCALDA Train
```
function [FFACE, TotalMeanFACE, pcaTotalFACE, projectPCA,eigvector, prototypeFACE]=PCALDA;
people = 40;
withinsample=5;
principlenum=50;
FFACE=[];
for k=1:1: people
    for m=1:2:10
        matchstring=['ORL3232' '\' num2str(k) '\' num2str(m) '.bmp'];
        matchX=imread(matchstring);
        matchX=double(matchX);
        if(k==1 && m==1)
               [row, col]=size(matchX);
        end
        matchtempF=[];
        
        %--arrange the image into a vector
        for n=1:row
               matchtempF=[matchtempF, matchX(n,:)];
        end
        
        FFACE=[FFACE; matchtempF];
    end   
end
TotalMeanFACE=mean(FFACE);
zeromeanTotalFACE=FFACE;
for i=1:1:withinsample*people
    for j=1:1:(row)*(col)
        zeromeanTotalFACE(i,j)=zeromeanTotalFACE(i,j)-TotalMeanFACE(j);
    end
end

SST = zeromeanTotalFACE' * zeromeanTotalFACE;
[PCA, latent] = eig(SST);
eigvalue=diag(latent);
[junk, index] = sort(eigvalue, 'descend');
PCA=PCA(:, index);
eigvalue=eigvalue(index);

projectPCA=PCA(:,1:principlenum);

pcaTotalFACE=[];
for i=1:1:withinsample*people
    tempFACE=zeromeanTotalFACE(i,:);
    tempFACE=tempFACE*projectPCA;
    pcaTotalFACE=[pcaTotalFACE;tempFACE];
end


for i=1:withinsample:withinsample*people
    withinFACE=pcaTotalFACE(i:i+withinsample-1,:);
    if(i==1)
        SW=withinFACE'*withinFACE;  % sw = cov(withinFACE)
        ClassMean=mean(withinFACE);
    end
    if(i>1)
        SW=SW+withinFACE'*withinFACE; % sw=sw+cov(withinFACE)
        ClassMean=[ClassMean;mean(withinFACE)];
    end
end
pcaTotalmean=mean(pcaTotalFACE);

% for i=1:people
%    for j=1:principlenum
%         ClassMean(i,j)=ClassMean(i,j)-pcaTotalmean(j);
%    end
% end

SB=ClassMean' * ClassMean;

[eigvector, eigvalue] = eig(inv(SW)*SB); % eigvector is LDA
eigvalue=diag(eigvalue);
[junk, index]=sort(eigvalue, 'descend');
eigvector=eigvector(:, index);

prototypeFACE=pcaTotalFACE*eigvector(:,1:30);
% prototypeFACE=prototypeFACE(1:10,:);


```

# PCALDA Test

```
function [correct,all_pred]=PCALDA_Test(projectPCA, LDA, prototypeFACE, TotalMeanFACE);


principlenum=50;
ldanum=50;

people=40;
withinsample=5;
totalcount=0;
correct=0;

projectLDA=LDA(:, 1:ldanum);

% Testing...
all_people=[];
all_pred=[];
org_pred=[];
for i=1:1:people
    for j=2:2:10    
        totalcount=totalcount+1;
        tempF=[];
        s=['ORL3232' '\' num2str(i) '\' num2str(j) '.bmp'];
        test = imread(s);
        [row, col]=size(test);
        test = double(test);
        for k=1:row
            tempF=[tempF, test(k, :)];
        end
        tempF=tempF-TotalMeanFACE;
        tempF=tempF*projectPCA;
        tempF=tempF*LDA(:,1:30);
        all_dist = [];
        for k=1:1:200
            exp=prototypeFACE(k,:);
            oaf=tempF-exp;
            dist=oaf*oaf';
            all_dist = [all_dist, dist];
        end
        [vv, pred] = min(all_dist);
        org_pred=[org_pred, pred];
        pred=pred-1;
        pred=fix(pred/5);
        pred=pred+1;
        label=i;
        all_pred = [all_pred,pred];
        all_people=[all_people,label];
        if(pred == label)
            correct=correct+1;
        end
    end
end

```