Array problem on finding slopes

8 visualizaciones (últimos 30 días)
Mohamad Uzir
Mohamad Uzir el 13 de Jun. de 2021
Editada: Walter Roberson el 13 de Jun. de 2021
I have a problem with error;
"Index exceeds the number of array elements (10)."
The code goes like this:
A=[0,198.6026,397.2053,993.01346,2376.65783,3968.16977,4566.799,5161.454,6956.4044,9930.1346];
B=[0.29835,0.3978,0.467415,0.546975,0.745875,0.975375,1.09395,1.3923,2.56275,4.5288];
LS=zeros(1,5);
RS=zeros(1,5);
for n=1:5
for i=1:length(A)
LS(n)=B(i+1)-B(i)/(A(i+1)-A(i));
RS(n)=B(i+2)-B(i+1)/(A(i+2)-A(i+1));
LS(n+1)=B(i+3)-B(i+2)/(A(i+3)-A(i+2));
RS(n+1)=B(i+4)-B(i+3)/(A(i+4)-A(i+3));
LS(n+2)=B(i+5)-B(i+4)/(A(i+5)-A(i+4));
RS(n+2)=B(i+6)-B(i+5)/(A(i+6)-A(i+5));
LS(n+3)=B(i+7)-B(i+6)/(A(i+7)-A(i+6));
RS(n+3)=B(i+8)-B(i+7)/(A(i+8)-A(i+7));
LS(n+4)=B(i+9)-B(i+8)/(A(i+9)-A(i+8));
RS(n+4)=B(i+length(A))-B(i+9)/(A(i+length(A))-A(i+9));
end
end
  2 comentarios
Scott MacKenzie
Scott MacKenzie el 13 de Jun. de 2021
The error message is clear enough. The A array is of length 10, so
A(i+length(A))
makes no sense. The first time the loop executes, i=1. You are attempting to extract the 11th element from A but it only contains 10 elements.
Mohamad Uzir
Mohamad Uzir el 13 de Jun. de 2021
Hi Scott. Thank you for pointing that out. And the for loop for n is also not required. I just realized that about an hour I posted the question. The problem is now solved. Cheers.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by