Error Masg Matrix Dimensions exceeded

1 次查看(过去 30 天)
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

回答(3 个)

Marc Jakobi
Marc Jakobi 2016-10-8
编辑:Marc Jakobi 2016-10-8
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 个评论
Ken
Ken 2016-10-8
Tried both of the above but still get same error
Marc Jakobi
Marc Jakobi 2016-10-8
编辑:Marc Jakobi 2016-10-8
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.

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-10-8
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 2016-10-8
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.

类别

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