Loop Principal Component Analysis
5 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Kevin Holst
on 21 Jun 2012
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
George McFadden
on 17 Nov 2012
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 Comment
George McFadden
on 17 Nov 2012
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
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!