how to debug Index exceeds matrix dimensions
이전 댓글 표시
figure;
load('usborder.mat','x','y','xx','yy');
rng(3,'twister')
nStops = 200;
stopsLon = zeros(nStops,1);
stopsLat = stopsLon;
n = 1;
while (n <= nStops)
xp = rand*1.5;
yp = rand;
if inpolygon(xp,yp,x,y)
stopsLon(n) = xp;
stopsLat(n) = yp;
n = n+1;
end
end
plot(x,y,'Color','red');
hold on
plot(stopsLon,stopsLat,'*b')
hold off
idxs = nchoosek(1:nStops,2);
dist = hypot(stopsLat(idxs(:,1)) - stopsLat(idxs(:,2)), ...
stopsLon(idxs(:,1)) - stopsLon(idxs(:,2)));
lendist = length(dist);
tsp = optimproblem;
trips = optimvar('trips',lendist,1,'Type','integer','LowerBound',0,'UpperBound',1);
ERROR:-
Index exceeds matrix dimensions.
Error in sym/subsref (line 841)
R_tilde =
builtin('subsref',L_tilde,Idx);
댓글 수: 3
Walter Roberson
2018년 3월 28일
Please show the complete error message -- everything in red.
The error message is occurring in one of the routines in the symbolic toolbox, but it is not obvious that you have any symbolic variables at all.
MAYANK KUMAR
2018년 3월 29일
MAYANK KUMAR
2018년 3월 29일
답변 (1개)
the cyclist
2018년 3월 28일
It's difficult to answer that without knowing how much you know about debugging MATLAB in general. I would recommend looking at this documentation page about debugging.
Then, specifically, a very powerful technique is to type
dbstop if error
into the command window, and execute your code. That command will make MATLAB to stop execution at the line of the code that throws the error, and put you into the editor window at the offending line. Then, you can see the values of all the variables, their size, etc.
After you are done debugging, you will then want to type
dbclear if error
to go back to normal operation.
댓글 수: 9
MAYANK KUMAR
2018년 3월 29일
Is that the whole stack displayed by the error message? I would expect more. In particular, I would expect to see at the bottom of the stack which line of your code is ending up calling syms/subsref.
In any case, the error message is clear, Idx exceeds the number of elements in L_tilde.
Since you're now stopped in the debugger, what does
dbstack
show?
Also, clearly you're using some symbolic variable here, yet the code you show is not designed for symbolic variables. Are you sure that your load does not load symbolic variables?
MAYANK KUMAR
2018년 3월 29일
MAYANK KUMAR
2018년 3월 29일
Walter Roberson
2018년 3월 29일
Look above the code for syms, to the upper right corner. Do you see the small downward pointing triangle in a circle? Right click on that and choose the "Undock editor" menu item. When that is done, you will see that the error message has a number of more lines than what you have posted: we need all of the lines in red.
MAYANK KUMAR
2018년 3월 29일
Guillaume
2018년 3월 29일
When you're stopped in the debugger because of the error (i.e. you've issued dbstop if error, ran the code, and you now have a K>> prompt (not plain >>), what does
dbstack
show?
MAYANK KUMAR
2018년 3월 29일
Guillaume
2018년 3월 29일
Yes, then type
dbstack
and show the result
카테고리
도움말 센터 및 File Exchange에서 Debug Code에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



