MATLAB: ” Attempted to access sorted_evalue(1); index out of bounds because numel(sorted_evalue)=0. ” how to remove this??

MATLAB

%calculate eigen vector & eigen value of cm = evector & evalue
evector = dfdb * rvector;
evalue = diag( rvalue );
%clear rvector rvalue
disp( 'Calculated Eigen Vectors & Eigen Values of CM' );
[sorted_evalue, index] = sort( evalue ); %sorted in acending order
sorted_evalue = flipud( sorted_evalue ); %rearranged in decending order
index = flipud( index ); %rearranged corresponding indies also
%Now rearrange eigenvectors in the order of rearranged eigen values
evector( :, 1:nof ) = evector ( :, index );
smallest_evalue = (1/100) * sorted_evalue(0); % to obtain the effective eigen values
for i = 1:nof
if (sorted_evalue(i) < smallest_evalue )
break;
end
evalue = [ ];
end
index = i+1;

Best Answer

%calculate eigen vector & eigen value of cm = evector & evalue
if isempty(dfdb) || isempty(rvector)
error('Need a non-empty dfdb and rvector');
end
evector = dfdb * rvector;
evalue = diag( rvalue );
%clear rvector rvalue
disp( 'Calculated Eigen Vectors & Eigen Values of CM' );
[sorted_evalue, index] = sort( evalue, 'descending' );
%Now rearrange eigenvectors in the order of rearranged eigen values
if nof ~= length(index)
error( sprintf('nof found to be %d, needs to match number of indices = %d', nof, length(index)) );
end
evector( :, 1:nof ) = evector ( :, index );
%sorted-evalue are in descending order. Are you *sure* you want to take a fraction
%of the largest of them and name that as "smallest_evalue" ??
smallest_evalue = (1/100) * sorted_evalue(1); % to obtain the effective eigen values
%What is the point of this loop? If it is to find the _second_ location where an eigen value
%is less than 1/100 of the maximum value, then there would be much more straightfoward
%ways of doing this
for i = 1:nof
if (sorted_evalue(i) < smallest_evalue )
break;
end
%and why the heck is evalue left alone if the largest eigenvalue is negative
%but cleared otherwise? Because the only way that
% sorted_evalue(1) < sorted_evalue(1)/100 immediately is if sorted_evalue(1) < 0
evalue = [ ];
end
%and if we reached the end of the loop without using the "break" then is it
%truly okay that index will point past all of the values??
index = i+1;