Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
PCA matlab code (if statement behaves weirdly)

Subject: PCA matlab code (if statement behaves weirdly)

From: anirbid.banerjee@gmail.com

Date: 21 Sep, 2008 02:32:43

Message: 1 of 2

Hello,

I am using the following matlab code to implement the principal
component analysis algorithm for face recognition. I have used nImg =
100 images for the training set and I have generated eigenvectors(V)
and eigenvalues (D) corresponding to these 100 images. Now I would
like to construct eigenfaces from this set. The matrices v,d
correspond to the eigenvectors and eigenvalues of the eigenfaces.
However, I am stuck at this particular point. I am comparing the
eigenvalues of D w.r.t a number say 0.05, I am getting the entire V,
D. The if condition is returning values greater/less than 0.05. Where
I am mistaken?

Thanks for your advice.

...
...
 % Find Eigenvectors and eigenvalues V, D
[V, D] = eig(C);
% Find Eigenfaces - that is eliminate all the eigenvectors that have
% v,d are the non-zero eigenvectors and eigenvalues corresponding to
nImg'
% where nImg' < nImg
v=[];
d=[];
for icnt = 1:size(V,2)
 if (D(icnt,icnt) >= 0.05)
   v = [v V(:,icnt)];
   d = [d D(icnt,icnt)];
 end
end
close all;

Regards,
Anirbid

Subject: PCA matlab code (if statement behaves weirdly)

From: Greg Heath

Date: 21 Sep, 2008 09:11:45

Message: 2 of 2

On Sep 20, 10:32 pm, "anirbid.baner...@gmail.com"
<anirbid.baner...@gmail.com> wrote:
> Hello,
>
> I am using the following matlab code to implement the principal
> component analysis algorithm for face recognition. I have used nImg =
> 100 images for the training set and I have generated eigenvectors(V)
> and eigenvalues (D) corresponding to these 100 images. Now I would
> like to construct eigenfaces from this set. The matrices v,d
> correspond to the eigenvectors and eigenvalues of the eigenfaces.
> However, I am stuck at this particular point. I am comparing the
> eigenvalues of D w.r.t a number say 0.05, I am getting the entire V,
> D. The if condition is returning values greater/less than 0.05. Where
> I am mistaken?
>
> Thanks for your advice.
>

What is C? How is it determined from 100 images?

> ...
> ...
> % Find Eigenvectors and eigenvalues V, D
> [V, D] = eig(C);

C must be square

> % Find Eigenfaces - that is eliminate all the eigenvectors that have
> % v,d are the non-zero eigenvectors and eigenvalues corresponding to
> nImg'
> % where nImg' < nImg
>
> v=[];
> d=[];
> for icnt = 1:size(V,2)
> if (D(icnt,icnt) >= 0.05)
> v = [v V(:,icnt)];
> d = [d D(icnt,icnt)];
> end
> end
> close all;

d = fliplr(diag(D)); % Monotonically nonascending order
v = fliplr(V);
t = sum(d) % = trace(D)

You have to determine the pseudorank, r, by specifying cutoff
values for eigenvalues or pseudotrace = sum of eigenvalues.

Three criteria for determining the pseudorank, r, come to mind

1. Use the default tol defined to determine rank(C) = rank(D)

help rank

tol = max(size(C)) * norm(C) * eps

d(r) > tol >= d(r+1)


2. Specify d(r+1) as a specified % of d(1) = max(d)

d(r) > 0.01*d(1) >= d(r+1)


3. Specify sum(d(1:r)) as a specified % of t = sum(d)

sum(d(1:r)) >= 0.95 > sum(d(1:r-1)

I prefer the third criterion.

Hope this helps.

Greg

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us