Index exceeds the number of array elements error

6 vues (au cours des 30 derniers jours)
Hannah Pike
Hannah Pike le 29 Nov 2020
Commenté : Hannah Pike le 29 Nov 2020
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

Réponse acceptée

Image Analyst
Image Analyst le 29 Nov 2020
Modifié(e) : Image Analyst le 29 Nov 2020
Try this:
while (i <= length(Payload)) && (Payload(i) <= MaxPayload)
  1 commentaire
Hannah Pike
Hannah Pike le 29 Nov 2020
That worked great! Thank you so much!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 29 Nov 2020
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 commentaire
Hannah Pike
Hannah Pike le 29 Nov 2020
So woud you suggest I increment it by 1, or at least a smaller increment?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by