IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
eig_ordered.m
Go to the documentation of this file.
1 %>@ingroup maths
2 %>@file
3 %>@brief Returns eigenvectors of matrix ordered in descending order of eigenvalue.
4 %>
5 %> @sa MATLAB's <code>eig()</code> function
6 %
7 %> @param varargin see MATLAB's <code>eig()</code> function
8 %> @return <em>[vectors, lambdas]</em>
9 function [vv, ll] = eig_ordered(varargin)
10 
11 
12 % [vv, dd] = eigs(A, r); % eigenvectors, eigenvalues
13 [vv, dd] = eig(varargin{:}); % eigenvectors, eigenvalues
14 
15 % the eig() function gives eigenvalues in ascending order, however I have
16 % experienced the opposite with a low-rank matrix.
17 
18 % Also this time I am not taking into account the presence of NaN eigenvalues.
19 
20 % This creates an index vector at column 2 of the 'lambdas' variable
21 lambdas = diag(dd);
22 lambdas(:, 2) = (1:numel(lambdas))';
23 lambdas = sortrows(lambdas);
24 
25 % However, the index vector is in ascending order and we want the opposite
26 indexes = lambdas(end:-1:1, 2);
27 vv = vv(:, indexes);
28 % vv = vv(:, 1:r);
29 
30 ll = lambdas(end:-1:1, 1)';
31 % ll = ll(1:r);
function eig_ordered(in varargin)