comparison toolboxes/FullBNT-1.0.7/netlab3.3/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 [temp_evec, temp_evals] = eig(x);
33 else
34 options.disp = 0;
35 [temp_evec, temp_evals] = eigs(x, N, 'LM', options);
36 end
37 temp_evals = diag(temp_evals);
38 end
39
40 % Eigenvalues nearly always returned in descending order, but just
41 % to make sure.....
42 [evals perm] = sort(-temp_evals);
43 evals = -evals(1:N);
44 if ~evals_only
45 if evals == temp_evals(1:N)
46 % Originals were in order
47 evec = temp_evec(:, 1:N);
48 return
49 else
50 % Need to reorder the eigenvectors
51 for i=1:N
52 evec(:,i) = temp_evec(:,perm(i));
53 end
54 end
55 end