Array problem on finding slopes

조회 수: 8 (최근 30일)
Mohamad Uzir
Mohamad Uzir 2021년 6월 13일
편집: Walter Roberson 2021년 6월 13일
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
Scott MacKenzie
Scott MacKenzie 2021년 6월 13일
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 2021년 6월 13일
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.

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

답변 (0개)

카테고리

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

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by