Question with for loop 'Index in position 1 exceeds array bounds (must not exceed 9)'

1 view (last 30 days)
Hi all,
I don't know how to make sure index i not exceed the array bound in this case (since lats is a 9x13 matrix, NumCenPts is actually the matrix's number of columns and depends on the situation number of rounds can be larger then number of columns and vice versa). need_to_modify_code.m is attached below
NumCenPts = 13;
load('need_to_modify_code');
V=10;
for i=1:length(lats)-1
for j=1:NumCenPts
%------------------------------------------------------
%Defining arcs and cost for going straight
%------------------------------------------------------
arcStraight(i,j)=distance('rh',lats(i,j), lons(i,j),lats(i+1,j),lons(i+1,j),referenceEllipsoid(7030));
%Defining arcs and cost for going right
%------------------------------------------------------
if j<NumCenPts
arcRight(i,j)=distance('rh',lats(i,j),lons(i,j), lats(i+1,j+1),lons(i+1,j+1),referenceEllipsoid(7030));
end
%-------------------------------------------------------
%Defining arcs and costs for going left
%------------------------------------------------------
if j>1
arcLeft(i,j)=distance('rh',lats(i,j),lons(i,j), lats(i+1,j-1),lons(i+1,j-1),referenceEllipsoid(7030));
end
end
end
Thank you for your time,
Sincerely,

Accepted Answer

per isakson
per isakson on 4 Jun 2019
Edited: per isakson on 4 Jun 2019
Line 9
arcStraight(ii,jj)=distance('rh',lats(ii,jj), lons(ii,jj),lats(ii+1,jj),lons(ii+1,jj),referenceEllipsoid(7030));
contains lats(ii+1,jj) and lons(ii+1,jj), which triggers the error when ii==9, since the size of lats and lons are <9x13>. 9+1 is 10 which is "outside" lons

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!