Loop Principal Component Analysis
조회 수: 5 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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
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
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!