function [ d ] = cumVariance( eigvals, t_d ) % % The cummulative variance method for selecting the number of components to % take in principle component analysis (PCA). This method selects the number % of PCA eigenvectors to include such that the percentage of total variances % included exceedes the threshold t_d. This is taken from page 38 of % % Exploratory Data Analysis with MATLAB % by Martinez and Martinez. % % Written by: % -- % John L. Weatherwax 2005-08-14 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- % Pick a threshold between 70-95% from Jackson [1991] if( nargin < 2 ) t_d = 0.85; end % make sure we have a row vector (with postive values): eigvals = abs(eigvals(:).'); % Ensure that the eigenvalues are sorted in decreasing order: eigvals = sort( eigvals ); eigvals = fliplr( eigvals ); % Compute the total variance: tv = sum( eigvals ); % Find the sums of variances that are greater than t_d threshold: inds = ( cumsum( eigvals ) / tv ) > t_d ; % Extract the indices: cumVarL = find( inds ); % Send out the first one: d = cumVarL(1);