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

조회 수: 3 (최근 30일)
%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;

채택된 답변

Walter Roberson
Walter Roberson 2016년 2월 24일
%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;

추가 답변 (1개)

Guillaume
Guillaume 2016년 2월 24일
It would be helpful to know which line is causing the error. It should be part of the error message.
With the code you've posted, the only line I can see could generate this error is the if test within the for loop. The thing is, there's no way your code could reach that line because another error will occur before that.
Anyway, the error message is very clear, sorted_evalue has 0 elements. It's empty. If it's empty that means that evalue and in turn rvalue are empty. Only you can tell why they are empty.
But as I said, I don't see how your code can even generate the error, since it will fail on the line.
smallest_evalue = ... sorted_evalue(0)
This will throw the error "Subscript indices must either be real positive integers or logicals." since indexing in matlab starts at 1.
  댓글 수: 1
Guillaume
Guillaume 2016년 2월 24일
By the way, learn to use the debugger. Step through your program and see how the variables evolve. You'll quickly see where it goes wrong.
You'll also find out that your loop probably does not do what you want (it does nothing, and if it did anything it would empty the whole evalue.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by