"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...
