Effacer les filtres
Effacer les filtres

I am trying to create a while loop where a marker can't leave a box of x = -10 and y = 10, y = -10. Once the marker reaches x = 11 I want the loop to stop.

1 vue (au cours des 30 derniers jours)

Réponse acceptée

Torsten
Torsten le 9 Mar 2024
Déplacé(e) : Torsten le 9 Mar 2024
Put the k = k+1 at the end of the while-loop, not at the beginning.
And if x(k) == -10, you only set x(k+1), but not y(k+1). This will lead to an access error for y(k+1) after k is increased by 1 for the next step.
  1 commentaire
Voss
Voss le 9 Mar 2024
To avoid that error: whatever value k has, x has to have at least k elements. That means, since you are incrementing k to k+1 on each iteration of the loop, you need to assign x(k+1) on each iteration of the loop.
But in this case x(k+1) is not assigned:
else if x(k) == -15 & y(k) == -15
y(k+1) = y(k) + 1;
y(k+1) = y(k) + 1;
k = k+1
Maybe it should be this instead?
else if x(k) == -15 & y(k) == -15
x(k+1) = x(k) + 1;
y(k+1) = y(k) + 1;
k = k+1

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by