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 次查看(过去 30 天)

采纳的回答

Torsten
Torsten 2024-3-9
移动:Torsten 2024-3-9
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 个评论
Voss
Voss 2024-3-9
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by