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

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

采纳的回答

per isakson
per isakson 2019-6-4
编辑:per isakson 2019-6-4
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by