IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
princomp2.m
Go to the documentation of this file.
1 %>@ingroup maths
2 %>@file
3 %>@brief Principal Component Analysis (PCA)
4 %>
5 %> PCA formula:
6 %>@code
7 %> scores = X*loadings
8 %>@endcode
9 %>
10 %> Loadings are the eigenvectors of the X's scatter matrix. The scatter matrix is defined ad X'*X, a simmetric positive definite or semi-definite
11 %> with rank <code>r <= @ref nf</code>.
12 %>
13 %> Meanings of the possible outputs:
14 %> @arg @c scores: <code>[@ref no][r]</code> PCA scores (@c r is the rank of the dataset's scatter matrix).
15 %> @arg @c loadings: <code>[@ref nf][r]</code> loadings matrix.
16 %> @arg @c lambdas: <code>[r]x[1]</code> contains the eigenvalues of the scatter matrix.
17 %>
18 %> Note: the loadings vectors sometimes happen to point at the opposite directions of those obtaines by MATLAB's princomp() (not really a problem).
19 %>
20 %> <h3>References</h3>
21 %> [1] R. O. Duda, P. E. Hart, and D. G. Stork, Pattern Classification, 2nd ed. New York: John Wiley & Sons, 2001.
22 %
23 %> @param X [@ref no]x[@ref nf] matrix
24 %> @return <code>[loadings]</code> or <code>[loadings, scores]</code> or <code>[loadings, scores, lambdas]</code>
25 function [varargout] = princomp2(X)
26 
27 nf = size(X, 2);
28 
29 if 0
30  X = data_normalize(X, 'c'); % center
31 end;
32 
33 cc = cov(X); % Covariance matrix
34 r = rank(cc);
35 
36 [vv, ll] = eig_ordered(cc); % eigenvectors, eigenvalues
37 
38 vv = adjust_unitnorm(vv);
39 
40 if nargout() == 1
41  varargout = {vv};
42 elseif nargout() == 2
43  varargout = {vv, X*vv};
44 elseif nargout() == 3
45  varargout = {vv, X*vv, ll};
46 end;
function adjust_unitnorm(in L)
function eig_ordered(in varargin)