Index exceeds the number of array elements (6).

I get this error and do not solve the problem please inform me how to solve it.
related codes,
for i=1:6
tt(1)=0;
yol=sqrt((xr(i+1)-xr(i))^2+(yr(i+1)-yr(i))^2);
tt(i+1)=tt(i)+yol/Vr;
end
xrr=horzcat(tt',xr);
yrr=horzcat(tt',yr);
xr - yr values;
3 - 3
2.68060147430381 - 3.13671296448901
4.36591862294010- 6.36978774648130
6.81142757321166- 7.19528445914528
7.75732360564317- 4.90764974391571
8- 5
thanks for helps.

1 comentario

Sriram Tadavarty
Sriram Tadavarty el 14 de Mayo de 2020
Hi Sinan,
At the loop 6, the code access xr(6+1) which is not present. Inorder to solve this, you can append zeros at the front of xr and yr. The other one, being looping from 1:5. The actual case depends on what you needed in the output.
Hope this helps.
Regards,
Sriram

Iniciar sesión para comentar.

Respuestas (1)

Ayush Goyal
Ayush Goyal el 19 de Jun. de 2020

0 votos

From my understanding of the question you are facing index issue in your code. Since you are iterating your for loop from i=1 to i=6, so when i=6 the code is accessing xr(6+1) and yr(6+1) but the length of both xr and vr vector is 6. You should iterate your for loop for i=1:5 or you can append zeros at the end of xr and yr. You can refer to the following link for how to append to array or vector:

Categorías

Preguntada:

el 14 de Mayo de 2020

Respondida:

el 19 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by