Index exceeds the number of array elements error

My code needs to make a new vector called Fuel for each value in the vector Payload, and I am getting an error that says "index exceeds the number of array elements. Could somone help me figure out where the error is an how to fix it?
Payload = [0:100:MaxPayload];
i = 1;
while Payload <= MaxPayload
Fuel(i) = MaxTakeOff - EmptyWeight - Payload(i) - TotalCrewWeight;
i = i + 1;
end

 채택된 답변

Image Analyst
Image Analyst 2020년 11월 29일
편집: Image Analyst 2020년 11월 29일

0 개 추천

Try this:
while (i <= length(Payload)) && (Payload(i) <= MaxPayload)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 11월 29일

0 개 추천

Suppose MaxPayload was 250, 0:100:250 is 0 100 200. All of the entries of that are less than MaxPayload so the loop would not terminate.
Even if MaxPayload were 200 then 0:100:200 is 0 100 200 and all entries of that are less than or equal to MaxPayload.
Your loop will never terminate until you get an error.

댓글 수: 1

So woud you suggest I increment it by 1, or at least a smaller increment?

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

질문:

2020년 11월 29일

댓글:

2020년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by