Why pca() returns 1 less principal component?

a = diag([1 2 3]);
disp(pca(a));
I expected that it will display 3 principal components, since my matrix "a" is full-rank 3-by-3 one. However, only two PCs are displayed. Actually, when I do the same in R, it does give me 3 PCs, the first two of which are exactly the same as the two that matlab provides.
However, when the number of row exceeds the number of columns, pca() function seems to work well, for example:
a = [diag([1 2 3]); 0, 0, 4];
disp(pca(a));
Is it because I didn't use pca() properly, or do I need to specify the style for the result to be displayed? Thanks...

 채택된 답변

KSSV
KSSV 2017년 7월 27일

3 개 추천

See you need not to use inbuilt function for pca. Pca is straight forward with few steps. Check the below pca code with out using inbuilt pca function.
a = diag([1 2 3]);
% disp(pca(a));
A = cov(a) ; % co-variance matrix
[V,D] = eig(A) ; % GEt Eigenvalues and Eigenvectors
Eig = diag(D) ;
[val,idx] = sort(Eig,'descend') ;
PV = Eig(idx)
PC = V(:,idx)
disp(pca(a))
As you see one of the principal values (Eigenvalues) is zero. So pca is not displaying it.

댓글 수: 2

Zachary L
Zachary L 2017년 7월 27일
Thank you for your answer!
So pca() doesn't use the raw input 'a' but the covariance 'cov(a)' instead, that makes sense. I see where my misunderstanding is, thank u!
KSSV
KSSV 2017년 7월 27일
Yes..... pca acts on the covariance matrix. See this tutorial http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf. It is amazing and very simple one to understand.

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

추가 답변 (1개)

Soma Mbadiwe
Soma Mbadiwe 2018년 9월 6일

3 개 추천

[coeff,score,latent] = pca(X, 'Economy',false);
The code above will tell MATLAB to return all the PCs and PVs even if some are zero.
No need re-inventing the wheel, especially because MATLAB's 'pca' function can give you so much more than just the PCs (coeff) and PVs (latent).
PS: I know this is more of an answer to the title of this question than the question itself, but Google brings this page up quite often.

댓글 수: 1

Changoleon
Changoleon 2019년 1월 29일
This is correct! Thank you for the clarification. If the number of observation is less than variables, pca function must be used with Economy as false.

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

카테고리

도움말 센터File Exchange에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

질문:

2017년 7월 27일

댓글:

2019년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by