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 more efficient

Subject: PCA more efficient

From: Jose

Date: 9 Dec, 2008 12:28:01

Message: 1 of 3

S=xsn;
Hi, I have this code to calcule the first components from the PCA.
Anyone, can help me to writte down it more efficently.

Specifically to obtain the vector Ex:
I have consider the sum of components (ax) reach the 65% of the total contribution of the components,
but there are other form more efficiently to write dow it?.

FT=cov(S);
[evecs,evals]=eig(FT);
[sev,I]=sort(diag(evals))
nsa=length(sev);

ax=0;
Ex=[];
for i=nsa:-1:1
    
    ax= ax+sev(i)/sum(sev);
    Ex=[Ex evecs(:,i)];
    
    if ax>0.85 % The sum of components have to account for 65% of the data variation.
    break % We do not have account the rest of components.
    end
end


cheers,
Jose.

Subject: PCA more efficient

From: Pekka Kumpulainen

Date: 9 Dec, 2008 13:34:02

Message: 2 of 3

"Jose " <jose.l.vega@gmail.com> wrote in message <ghlo8h$d2q$1@fred.mathworks.com>...
> S=xsn;
> Hi, I have this code to calcule the first components from the PCA.
> Anyone, can help me to writte down it more efficently.
>
> Specifically to obtain the vector Ex:
> I have consider the sum of components (ax) reach the 65% of the total contribution of the components,
> but there are other form more efficiently to write dow it?.
>
> FT=cov(S);
> [evecs,evals]=eig(FT);
> [sev,I]=sort(diag(evals))
> nsa=length(sev);
>
> ax=0;
> Ex=[];
> for i=nsa:-1:1
>
> ax= ax+sev(i)/sum(sev);
> Ex=[Ex evecs(:,i)];
>
> if ax>0.85 % The sum of components have to account for 65% of the data variation.
> break % We do not have account the rest of components.
> end
> end
>
>
> cheers,
> Jose.

Ex is growing inside a loop, which is not efficient at all. Don't do that ever again! ;-)
But you don't need the loop here.
[sev,I]=sort(diag(evals),'descend') % take the big ones first
ve = sev./sum(sev); % % of each component
ind = find(cumsum(ve)>.65,1,'first'); % the first where sum>.65
Ex = evecs(:,I(1:ind)); % note that you need to reorder evec too

Hope, I got it right...

Subject: PCA more efficient

From: Jose

Date: 9 Dec, 2008 14:15:04

Message: 3 of 3

Dear Pekka,
Thank you very much, it works reallly well.
But...there are other form more efficently to avoid the threshold 0.65?
or the only form to reduce component is including a theshold %...:(
because it looks like ambigous...what is the difference between to use 65, 70,80...% to reduce the dimensions of my problem?
You know if 65% is a good estimation, to keep the right autovectors (dimensions)?
Thanks in advance
Jose.
I mean...sometime
"Jose " <jose.l.vega@gmail.com> wrote in message <ghlo8h$d2q$1@fred.mathworks.com>...
> S=xsn;
> Hi, I have this code to calcule the first components from the PCA.
> Anyone, can help me to writte down it more efficently.
>
> Specifically to obtain the vector Ex:
> I have consider the sum of components (ax) reach the 65% of the total contribution of the components,
> but there are other form more efficiently to write dow it?.
>
> FT=cov(S);
> [evecs,evals]=eig(FT);
> [sev,I]=sort(diag(evals))
> nsa=length(sev);
>
> ax=0;
> Ex=[];
> for i=nsa:-1:1
>
> ax= ax+sev(i)/sum(sev);
> Ex=[Ex evecs(:,i)];
>
> if ax>0.85 % The sum of components have to account for 65% of the data variation.
> break % We do not have account the rest of components.
> end
> end
>
>
> cheers,
> Jose.

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