Loop Principal Component Analysis

조회 수: 10 (최근 30일)
stefan strasser
stefan strasser 2012년 6월 21일
Hello,
I would like to do a principal component analysis. The matrix is of 50x50 dimension. I don't want Matlab to run the PCA on the whole 50x50 Matrix but perform it from rows 1-10 then from 11-20 and so forth up until row 50. I tried around using loops. However, I did not arrive at my desired result which is a matrix that consists of 5 rows and 50 colums. They first row would contain the coefficients of the first component retrieved from the 1-10 pca, the second row would contain the first components for the 11-20 pca and so on…. Any chance somebody could give me a hint? Thank you very much Stefan

답변 (2개)

Kevin Holst
Kevin Holst 2012년 6월 21일
I'd suggest looking at the help page a little more for PCA:
"COEFF = princomp(X) performs principal components analysis (PCA) on the n-by-p data matrix X, and returns the principal component coefficients, also known as loadings. Rows of X correspond to observations, columns to variables. COEFF is a p-by-p matrix, each column containing coefficients for one principal component. The columns are in order of decreasing component variance."
EDIT
by first component of the 1-10 analysis, do you mean the first row of the coefficients? If so, I think this will work for you:
for i = 1:size(x,1)/10 % this assumes the number of rows is ALWAYS a multiple of 10
[COEFFnew,SCOREnew,latentnew] = princomp(x((i-1)*10+1,i*10)); % if you don't need SCOREnew or latentnew, you can just leave those off
coeff(i,:) = COEFFnew(1,:);
end
coeff should be a 5x50 for your example of a 50x50 matrix, but the same code will also expand to a 1000x1000
  댓글 수: 2
stefan strasser
stefan strasser 2012년 6월 21일
Thank you Kevin,
already checked on that one….if i just run the princomp command it performs pca on the whole matrix!however, i need princomp for rows 1-10,11-20,21-30,31-40 and 41-50. out of these individual results i need a matrix that consists of row 1= 1st component of the 1-10 analysis/2nd row 1st component of the 11-20 pca and so on and so on…finally it would be a 5x50 matrix consisting of the COEFFs as you mentioned correctly.... i need to work something out using a loop since my final data set will contain thousands of rows/assets and there is no way i could do that manually…
best regards,
stefan
stefan strasser
stefan strasser 2012년 6월 21일
%pca looped
this is what i had in mind as a loop…BUT: how to proceed so that i receive the desired matrix?
for t=11:10:50
[COEFFnew,SCOREnew,latentnew] = princomp(x(((t-10):(t-1)),:))
end

댓글을 달려면 로그인하십시오.


George McFadden
George McFadden 2012년 11월 17일
The easiest way would be to divide your matrix in to 5 matrices, each 10x50. Then do a for loop that calls each matrix.
Example:
%variable =50x50
v{1}=variable(1:10,:);v{2}=variable(11:20,:); %etc.
%then perform loop
for i=1:length(v)
[coeff,score,latent,explained]=pca(v{i});
end
  댓글 수: 1
George McFadden
George McFadden 2012년 11월 17일
Oops! You would have to assign the pca results to a new variable for each iteration. So...
Example: %variable =50x50 v{1}=variable(1:10,:);v{2}=variable(11:20,:); %etc. %then perform loop for i=1:length(v) [coeff{i},score{i},latent{i},explained{i}]=pca(v{i}); end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by