Loop Principal Component Analysis

14 次查看(过去 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