Why does my code only print the final iteration?
显示 更早的评论
function [ ] = forloopef(x , y, m, n)
while x < 49
[x] = 7 + x
end
while y < 49
[y] = 7 + y
end
while m > 7
[m] = m - 14
end
while n > 7
[n] = n - 14
end
hold on
rectangle('Position',[x y m n])
axis([0 100 0 100])
3 个评论
Walter Roberson
2017-3-19
What input are you passing? What does your call look like?
Guillaume
2017-3-19
Note 1: I would get out of the habit of writing brackets [] around the assignee in your code. Not only is it completely unnecessary, it is also likely to cause you problems in some future code.
x = 7 + x
works just as well.
Note 2: And of course none of the while loops are necessary:
x = max(x, mod(x, 7) + 49);
y = max(x, mod(y, 7) + 49);
m = min(m, mod(m + 6, 14) - 6;
n = min(m, mod(n + 6, 14) - 6;
Image Analyst
2017-3-19
编辑:Image Analyst
2017-3-19
Don't double space code to format it. Simply learn to format it. See the tutorial: http://www.mathworks.com/matlabcentral/answers/13205#answer_18099 Basically, paste in code, highlight it, and click the {}Code icon.

回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!