MATLAB Answers

Index in position 1 is invalid. Array indices must be positive integers or logical values.

5 views (last 30 days)
GHADAH AL-OBAIDI on 19 Apr 2021
Reopened: Walter Roberson on 28 Apr 2021
I am getting an error in line: while m(xo,yo)==0 %if the location not occupied
the error is '' Index in position 1 is invalid. Array indices must be positive integers or logical values''
and my code is:
xo=-13; yo=-13; th=pi/2;
x=-13; y=-13;
rr=1; %Robot radius
r=rr; %ray vector
u=Control(); % control function
Unrecognized function or variable 'Control'.
n=4; %Number of sensors
dT=0.4;
dbstop if error
for t=1:size(u,1)
v=u(1,t);
if u(2,t)~=0
w=u(2,t);
else
w=100*eps; % 100*eps because eps alone is too small, so causes division by zero problem
end
subplot(1,2,1);
m=Map(); %map function
x=x-v/w*sin(th)+v/w*sin(th+w*dT);%state update
y=y+v/w*cos(th)-v/w*cos(th+w*dT);%state update
th=th+w*dT;%state update
for k=0:n-1
th=th+2*pi*k/n;
rr=1; r=rr;
xo=xo+r*cos(th); %corridnates(x,y) for the robot location
yo=yo+r*sin(th); %r is length(r)
xo=ceil((xo*641*0.05)-1);
yo=ceil(yo);
while m(xo,yo)==0 %if the location not occupied %%% my error in this line
r=rr+0.05;
if m(xo,yo)==1 %if the location occupied
subplot(1,2,2);
mesh(xo,yo,m); view(0, 90); axis image; hold on;
plot(xo,yo,'b.');
end
end
end
end
1 CommentShowHide None

Sign in to comment.

Answers (2)

Paul Hoffrichter on 19 Apr 2021
Edited: Paul Hoffrichter on 19 Apr 2021
Would be very useful if you are able to post a minimal program exhibiting the error that we could run. In the debugger, set the Run button to "Pause on Error". When the program breaks at the error, check that xo and yo are scalars, and check that they are both greater than 0.
16 CommentsShowHide 15 older comments
Paul Hoffrichter on 21 Apr 2021
From your stated requirements, I do not understand your left and right output figures. I think you may have a requirements and design issue rather than a Matlab question.
For a general programming point of view (whether Matlab or not), you should get the program to run with a number of well defined functions each having a clearly stated purpose. There should be more comments so that all can understand your design. One function might be CreateInitialMaze(). Your main script should be fairly small and devoid of math computations. Design the main script with as many high-level functions as you need to do your job.
When I said, "Please put all your code in a code block.", I did not mean for you to eliminate your functions.
Unless you have a Matlab question, I recommend closing this question, and starting fresh with the general programming principles I suggested.

Sign in to comment.

Walter Roberson on 22 Apr 2021
You initialize a playing field of free space and obstacles.
You start at some particular location.
At any one point, you have a goal that you use to figure out which direction you would ideally like to go. You test to see if you can go in that direction. If so, you go there and loop back to the next step. If you cannot go in the most desired direction, you figure out the next best direction to go, and see if you can go there; if you cannot, then the next best after that, and so on. Unless this was the very first step and you happened to land in the one open spot surrounded by barriers, there is always somewhere you can go.
You will, however, find that in practice you need to be a bit more clever than the above.
1 R 2
-|_|-
3|G|4
5 6 7
Let R be the robot, and G be the goal. The "ideal" direction would be for the robot to move downward. But of course if it does, then it cannot go any further towards G, and needs to move back to [1], or the location it came from [R], or to [2] . If it moves back to [R] then it gets back to the place where the "ideal" move is straight down towards G... getting stuck again. If it moved to [1] or to [2], then in the next step, the "ideal" move would be to move diagonally down, getting stuck in the same rut again.
How to deal with this, how to avoid cycling through positions due to lack of long-term planning, or due to limited "visibility" not permitting you to know what the "best" step is, is key to robotics. You can research algorithms for this, or you can try to come up with one yourself.
0 CommentsShowHide -1 older comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by