An error about "index_to_​remove_exc​eeds_matri​x_dimensio​ns"

Got an error when I submitted a job to cluster as follows:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MATLAB:index_to_remove_exceeds_matrix_dimensions Error in ==> threednonlinear at 717 Index of element to remove exceeds matrix dimensions.
ans{1} =
MException
Properties:
identifier: 'globaloptim:fcnvectorizer:fitnessEvaluation'
message: [1x73 char]
cause: {}
stack: [0x1 struct]
Methods
ans =
{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Anyone has any idea about this error? Is it related to a bug or something I didn't do right?
Thanks
Best

 Accepted Answer

I can try this:
>> x=1:10;x(20:30)=[];
??? Index of element to remove exceeds matrix dimensions.
It means that you are trying to remove elements that are exceeds the matrix dimensions.

2 Comments

Hi Fangjun,
I double checked my codes. I think I exclude the reason you mentioned.
And the code did not stop immediately when it reached line 717. It ran a while and then it stopped.
Can you run "DBSTOP if error" and then debug, check the size of your matrix?

Sign in to comment.

More Answers (1)

Typically this happens when you have a "for" loop that iterates forward deleting the element at the current index if some criteria is met.
The problem is that when you delete the current element, all the rest of the elements shift downward and the length of the vector changes, so in later iterations you are no longer referring to the element you thought you were; eventually you get to the point where you think you are trying to remove (say) the 5th last element, but there have been 6 prior shifts downward so you are trying to remove an element at an index that no longer exists.
In such a situation, the easiest cure is to loop backwards, deleting from the end first and moving on to earlier elements.

Community Treasure Hunt

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

Start Hunting!