Hey guys i have a problem with arrray indexing.

1 view (last 30 days)
Musie Frdiesa
Musie Frdiesa on 14 May 2020
Commented: Musie Frdiesaon 15 May 2020
Index exceeds array bounds. Error in mywork (line 190)
for k=1:Noants tour=ant(k).Tour; for l=1:noNodes tau(tour(l),l)=tau(tour(l),l)+Q/ant(k).Cost; end end

Answers (1)

Steven Lord
Steven Lord on 14 May 2020
Set an error breakpoint then run your code. When it stops, MATLAB will be paused in the location where the error occurred. To make it clearer to understand which instruction caused the error, you may want to rewrite this line so each instruction is on its own line.
fork=1:Noants
tour=ant(k).Tour;
forl=1:noNodes
tau(tour(l),l)=tau(tour(l),l)+Q/ant(k).Cost;% Or maybe even
% temp = Q/ant(k).Cost;
% tau(tour(l),l)=tau(tour(l),l)+temp;
end
end
Look at the size of each of the arrays being indexed into on the line in question. Depending on the specific line, some of the questions you may need to ask are:
  • Does ant have at least k elements?
  • Does tour have at least l elements?
  • Does tau have at least tour(l) rows and at least l columns?
The answer to at least one of those questions is going to be no. Once you know which one(s) work your way backwards through your code, adding breakpoints and/or conditional breakpoints on earlier lines and rerunning your code until you've identified where your code isn't doing what you expect and/or want it to do.
1 Comment
Musie Frdiesa
Musie Frdiesa on 15 May 2020
Thanks for your help sir. i will try my best.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!