Index exceeds matrix dimensions

I have the foll. program and when I run it, I get the above error msg i.e. 'Index exceeds matrix dimensions'. Not sure what causes this msg. Please help.
A=[2,2,3,4,5;2,3,6,8,9;3,4,7,6,4;2,9,0,4,5;3,8,7,9,2];
%x=1;
%y=1;
OptimalPath=[2,2];
CurrentPos=[2,2];
min=99;
for x=OptimalPath(end,1)-1:OptimalPath(end,1)+1;
for y= OptimalPath(end,2)-1:OptimalPath(end,2)+1
if A(x,y)<min
min=A(x,y);
end
CurrentPos(1)=CurrentPos(1)+x;
CurrentPos(2)=CurrentPos(2)+y;
OptimalPath=[OptimalPath;CurrentPos]
end
end

Respuestas (1)

Roche de Guzman
Roche de Guzman el 14 de Oct. de 2016

1 voto

Your y For Loop counter is becoming greater than 5 (the number of columns of your matrix A). Since you are using y for indexing, A(x,y), then you are exceeding the matrix dimensions.

3 comentarios

Star Strider
Star Strider el 15 de Oct. de 2016
Ken’s Comment moved here: (15 Oct 2016 at 00:10 UTC)
Thanks. How to determine that y is the culprit. I tried but could not as it just kept on giving me the same msg. Do you use breakpoints to determine that y is the problem?
Ken
Ken el 15 de Oct. de 2016
Anyone care to respond? Also how to correct this?
dbstop if error
then run your code. When it stops, examine the size() of each variable being indexed, and examine the value of each of the indexing expressions. The more difficult part is then trying to figure out either how the indexing expression got to be that wrong value, or trying to figure out why the variable being indexed is not as large as expected.
(Hint for the future: if the variable being indexed is not as large as expected, check to see if you are using the / operator somewhere that you need the ./ operator.)

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

Ken
el 14 de Oct. de 2016

Comentada:

el 15 de Oct. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by