comparison toolboxes/FullBNT-1.0.7/KPMstats/eigdec.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [evals, evec] = eigdec(x, N)
2 %EIGDEC Sorted eigendecomposition
3 %
4 % Description
5 % EVALS = EIGDEC(X, N computes the largest N eigenvalues of the
6 % matrix X in descending order. [EVALS, EVEC] = EIGDEC(X, N) also
7 % computes the corresponding eigenvectors.
8 %
9 % See also
10 % PCA, PPCA
11 %
12
13 % Copyright (c) Ian T Nabney (1996-2001)
14
15 if nargout == 1
16 evals_only = logical(1);
17 else
18 evals_only = logical(0);
19 end
20
21 if N ~= round(N) | N < 1 | N > size(x, 2)
22 error('Number of PCs must be integer, >0, < dim');
23 end
24
25 % Find the eigenvalues of the data covariance matrix
26 if evals_only
27 % Use eig function as always more efficient than eigs here
28 temp_evals = eig(x);
29 else
30 % Use eig function unless fraction of eigenvalues required is tiny
31 if (N/size(x, 2)) > 0.04
32 fprintf('netlab pca: using eig\n');
33 [temp_evec, temp_evals] = eig(x);
34 else
35 options.disp = 0;
36 fprintf('netlab pca: using eigs\n');
37 [temp_evec, temp_evals] = eigs(x, N, 'LM', options);
38 end
39 temp_evals = diag(temp_evals);
40 end
41
42 % Eigenvalues nearly always returned in descending order, but just
43 % to make sure.....
44 [evals perm] = sort(-temp_evals);
45 evals = -evals(1:N);
46 %evec=temp_evec(:,1:N);
47 if ~evals_only
48 if evals == temp_evals(1:N)
49 % Originals were in order
50 evec = temp_evec(:, 1:N);
51 return
52 else
53 fprintf('netlab pca: sorting evec\n');
54 % Need to reorder the eigenvectors
55 for i=1:N
56 evec(:,i) = temp_evec(:,perm(i));
57 end
58 end
59 end