Index exceeds the number of array elements (16)

조회 수: 4 (최근 30일)
Nicolás Ernesto Cortés Malagon
Hello I'm trying to execute the following code but in line 10 I get the error Index exceeds the number of array elements (16). How can I fix this?
Any help is much appreciated, Nicolás.
I=16;
J=8;
q=[17,4,12,13,16,15,14,11,10,9,8,7,6,5,3,2];
r=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
d0=[609.237228015492,855.902447712355,1494.20915537283,2181.40528100580,2721.82659256610,3177.38508840210,3832.36180442296,4596.64790907461,5562.93133159129,6394.14357048698,6970.81953575044,7852.66553470858,8700.10925218758,9257.26725335290,9773.34917006448,1411.75706125381]
d=[0,1458.47900224858,2097.18787904184,2783.99012210891,3324.71261915974,3780.43621821609,4435.35849734833,5199.83172804659,6166.15577162952,6997.33620744352,7574.04812501215,8455.87239733429,9303.34869818390,9860.49212767801,10376.5360308727,806.248100772957;1458.47900224858,0,638.855226166304,1325.99019604219,1866.63360089762,2322.32319025583,2977.31288244954,3741.73716340419,4708.07391615722,5539.28740904460,6115.99411379704,6997.84145290532,7845.31363299135,8402.46939893267,8918.53536181811,2264.43569129265;2097.18787904184,638.855226166304,0,687.216123210159,1227.79354942107,1683.47171048402,2338.46445343948,3102.88269194954,4069.21908970259,4900.43314412104,5477.13939935803,6358.98710487763,7206.45890850701,7763.61487968073,8279.68145522519,2903.24387539180;2783.99012210891,1325.99019604219,687.216123210159,0,540.722664588789,996.446185200184,1651.38790113044,2415.85119574861,3382.18420550981,4213.38165847814,4790.09331850644,5671.93132892139,6519.40764180305,7076.55862690333,7592.61568894409,3590.16002428861;3324.71261915974,1866.63360089762,1227.79354942107,540.722664588789,0,455.723600442198,1110.68492381953,1875.13626171540,2841.47232962068,3672.67586372661,4249.38642629733,5131.22831298706,5978.70362202376,6535.85656207356,7051.91732793288,4130.87811488066;3780.43621821609,2322.32319025583,1683.47171048402,996.446185200184,455.723600442198,0,654.996946557768,1419.42100872151,2385.75878914864,3216.96751615555,3793.67644903990,4675.52136130293,5522.99529241154,6080.14975144527,6596.21361085282,4586.59982993939;4435.35849734833,2977.31288244954,2338.46445343948,1651.38790113044,1110.68492381953,654.996946557768,0,764.476945368531,1730.79865957887,2561.99395003189,3138.70546563388,4020.54486357260,4868.02074769613,5425.17280830758,5941.23261621694,5241.54414271215;5199.83172804659,3741.73716340419,3102.88269194954,2415.85119574861,1875.13626171540,1419.42100872151,764.476945368531,0,966.337932609499,1797.55055561728,2374.25714698303,3256.10442093002,4103.57661071412,4660.73223860801,5176.79920414149,6006.01115550080;6166.15577162952,4708.07391615722,4069.21908970259,3382.18420550981,2841.47232962068,2385.75878914864,1730.79865957887,966.337932609499,0,831.218984383778,1407.92045229835,2289.76963906852,3137.23986969438,3694.39629709646,4210.46565120771,6972.34257620780;6997.33620744352,5539.28740904460,4900.43314412104,4213.38165847814,3672.67586372661,3216.96751615555,2561.99395003189,1797.55055561728,831.218984383778,0,576.712233267164,1458.55407853120,2306.02797034208,2863.18232042600,3379.24873307663,7803.53317414618;7574.04812501215,6115.99411379704,5477.13939935803,4790.09331850644,4249.38642629733,3793.67644903990,3138.70546563388,2374.25714698303,1407.92045229835,576.712233267164,0,881.850894426036,1729.31951934858,2286.47589097283,2802.54687739563,8380.24540213471;8455.87239733429,6997.84145290532,6358.98710487763,5671.93132892139,5131.22831298706,4675.52136130293,4020.54486357260,3256.10442093002,2289.76963906852,1458.55407853120,881.850894426036,0,847.476843341457,1404.62842061522,1920.69622793403,9262.07633309076;9303.34869818390,7845.31363299135,7206.45890850701,6519.40764180305,5978.70362202376,5522.99529241154,4868.02074769613,4103.57661071412,3137.23986969438,2306.02797034208,1729.31951934858,847.476843341457,0,557.158864238917,1073.24042040915,10109.5531553081;9860.49212767801,8402.46939893267,7763.61487968073,7076.55862690333,6535.85656207356,6080.14975144527,5425.17280830758,4660.73223860801,3694.39629709646,2863.18232042600,2286.47589097283,1404.62842061522,557.158864238917,0,516.086233104507,10666.6998176568;10376.5360308727,8918.53536181811,8279.68145522519,7592.61568894409,7051.91732793288,6596.21361085282,5941.23261621694,5176.79920414149,4210.46565120771,3379.24873307663,2802.54687739563,1920.69622793403,1073.24042040915,516.086233104507,0,11182.7484993628;806.248100772957,2264.43569129265,2903.24387539180,3590.16002428861,4130.87811488066,4586.59982993939,5241.54414271215,6006.01115550080,6972.34257620780,7803.53317414618,8380.24540213471,9262.07633309076,10109.5531553081,10666.6998176568,11182.7484993628,0]
DelPos=find(q>=I);
Desde=[0 DelPos]+1;
Hasta=[DelPos I+J]-1;
L=cell(J,1);
D=zeros(1,J);
UC=zeros(1,J);
for j=1:J
L{j}=q(Desde(j):Hasta(j)); (In this Line broke my code) Index exceeds the number of array elements (16)
if ~isempty(L{j})
D(j)=d0(L{j}(1));
for k=1:numel(L{j})-1
D(j)=D(j)+d(L{j}(k),L{j}(k+1));
end
D(j)=D(j)+d0(L{j}(end));
UC(j)=sum(r(L{j}));
end
end
  댓글 수: 4
KSSV
KSSV 2020년 8월 20일
UC(j)=sum(r(L{j}));
r is not defined.
Nicolás Ernesto Cortés Malagon
Thank you very much for the observation. I've already added it to the code.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2020년 8월 20일
Desde(j):Hasta(j)
In the above
Desde = [1 2 6] ;
Hasta = [0 4 23] ;
For j = 3
Desde(j):Hasta(j)
= 6:23
= 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
You have used:
q(Desde(j):Hasta(j))
But size of q is 16, and your code tries to access more than present elements; you cannot find q([17 18 19 20 21 22 23]). So the error. You need to think on your code logic.
  댓글 수: 1
Nicolás Ernesto Cortés Malagon
KSSV thank you very much for your time and clearing up my question!
Best wishes to you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by