Loop Principal Component Analysis

5 Ansichten (letzte 30 Tage)
stefan strasser
stefan strasser am 21 Jun. 2012
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

Antworten (2)

Kevin Holst
Kevin Holst am 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
  2 Kommentare
stefan strasser
stefan strasser am 21 Jun. 2012
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 am 21 Jun. 2012
%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

Melden Sie sich an, um zu kommentieren.


George McFadden
George McFadden am 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 Kommentar
George McFadden
George McFadden am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dimensionality Reduction and Feature Extraction finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by