Error Masg Matrix Dimensions exceeded

1 view (last 30 days)
Ken
Ken on 8 Oct 2016
Answered: Image Analyst on 8 Oct 2016
I am struggling over the above error msg. My code is below. Any hints app[reciated.
OptimalPath=zeros(40, 2);
iter=0;
M=1;
Row=1;
Col=1;
OptimalPath(M,1)=SearchSolution(SearchStart(1));
OptimalPath(M,2)=SearchSolution(SearchStart(2));
CurrentPos(M,1)=SearchSolution(SearchStart(1));
CurrentPos(M,2)=SearchSolution(SearchStart(2));
Min=SearchSolution(SearchStart(1),SearchStart(2));
while (Row < 8 & Col < 7)
while not(SearchSolution(Row,Col) == 1)
while not(SearchSolution(Row,Col) == 0)
iter = iter+1;
assert(maxIter>iter, 'maxIter assert triggered. Aborting.');
for X=1:1:3
for Y=1:1:3
if Min>CurrentPos(M(1),M(2));
Min=CurrentPos(M(1),M(2));
Row=X;
Col=Y; %capture the row and col corresp to the local min
end
end
end
CurrentPos(M,1)=Row;
CurrentPos(M,2)=Col;
end
end
Row=Row+1;
Col=Col+1;
M=M+1;
end

Answers (3)

Marc Jakobi
Marc Jakobi on 8 Oct 2016
Edited: Marc Jakobi on 8 Oct 2016
This is causing your error:
if Min>CurrentPos(M(1),M(2));
Min=CurrentPos(M(1),M(2));
M is a 1x1 matrix, so M(2) exceeds M's dimensions.
You probably mean
if Min>CurrentPos(M,2);
Min=CurrentPos(M, 2);
or
if Min>CurrentPos(M,1);
Min=CurrentPos(M, 1);
  2 Comments
Ken
Ken on 8 Oct 2016
Tried both of the above but still get same error
Marc Jakobi
Marc Jakobi on 8 Oct 2016
Edited: Marc Jakobi on 8 Oct 2016
Go through the code line for line and see where the error shows up. Refer to Image Analyst's links (answer below) if you are unsure how to debug. "Index exceeds matrix dimensions" is a very common error in Matlab that should be easy to find.

Sign in to comment.


Image Analyst
Image Analyst on 8 Oct 2016
Why are you saying this:
Row=X;
Col=Y; %capture the row and col corresp to the local min
This is opposite to the convention that the whole world uses.
You made the very common beginner mistake of thinking (x,y) = (row, column). It does not. If you're expecting (row, column), like you're indexing an array, then you need to use (y, x), NOT (x, y). Is that what you're doing? Take a very careful look at what your first index means and what you're passing in. If it's x, then pass in column. If it's row, then pass in y. Just make sure you're consistent.

Image Analyst
Image Analyst on 8 Oct 2016
The solution can be found here: in this link.
If that doesn't work, try this link before you ask again here, or call the Mathworks on the telephone for an immediate solution.

Community Treasure Hunt

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

Start Hunting!