to solve the problem: Index in position 1 exceeds array bounds. Index must not exceed 10

7 views (last 30 days)
i have this code:
x = [0 0 71 355 676];
v = [0 7 30 70];
xq = z1; %distance
vq2 = interp1(x,v,xq,'linear'); % velocity
vq2(isnan(vq2))=100;
d= [-160 -125 -89 -53 -17 18 54 90 126 161]; % reference angles on the grid
vel_thr= [0:100]; % sums of speeds to be considered
for i=1:length(z2) % z2 is angles
angsel=z2(i)*180/pi;
[tmp1, icol1]=min(abs(angsel-d));
[tmp2, icol2]=min(abs(360+angsel-d));
if tmp1<tmp2
icol(i)= icol1;
else
icol(i)= icol2;
end
velsel=vq2(i);
itmp=find(velsel>=vel_thr);
if isempty(itmp);
irow(i)=1;
else
irow(i)=itmp(end);
end
probfile=[cnorm(1,:)*100;cumsum(cnorm(2:end,:)*100,'reverse')]; %cnorm is 10x10 double
probfile2=fliplr(probfile);
prob(i)=probfile2(irow(i),icol(i));
end
everything is correct but on the line "prob(i)=probfile2(irow(i),icol(i))" it stops and the this error:
"Index in position 1 exceeds array bounds. Index must not exceed 10."
how can I solve this problem?
thanksss
  1 Comment
Adam
Adam on 3 Nov 2021
Easiest way to error check these is just to use the command line and check what the following values are at the point when the code errors:
irow(i)
icol(i)
size(probfile2)
irow(i) and icol(i) must be less than or equal to the 1st and 2nd elements of that size output, respectively.
It is too hard for me to tell by just glancing at the code what values these may have and in what iteration (i) they may fail, especially since z2, whose size defines the range over which i iterates appears to not be defined in this part of the code.

Sign in to comment.

Accepted Answer

Jon
Jon on 3 Nov 2021
From the error message it looks like the variable irow(i), that is the "Index in position 1" is bigger than 10, which apparently is the row dimension of the variable probfile2. So MATLAB can't execute that line because you are asking for row 11 or maybe some bigger number of an array that only has 10 rows.
I tried running your code to see if I could give you more specific advice but the snippet you have doesn't define the variable z1, so it wouldn't run. If you want to attach more code to demonstrate the problem then please attach it, but use the code button in the MATLAB Answers toolbar so it will be nicely formatted and easily copied

More Answers (0)

Community Treasure Hunt

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

Start Hunting!