Index exceeds the number of array elements error

1 回表示 (過去 30 日間)
Hannah Pike
Hannah Pike 2020 年 11 月 29 日
コメント済み: Hannah Pike 2020 年 11 月 29 日
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 日
Try this:
while (i <= length(Payload)) && (Payload(i) <= MaxPayload)
  1 件のコメント
Hannah Pike
Hannah Pike 2020 年 11 月 29 日
That worked great! Thank you so much!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 11 月 29 日
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 件のコメント
Hannah Pike
Hannah Pike 2020 年 11 月 29 日
So woud you suggest I increment it by 1, or at least a smaller increment?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by