Why does my code only print the final iteration?

2 ビュー (過去 30 日間)
Bennie Forbes
Bennie Forbes 2017 年 3 月 19 日
編集済み: Image Analyst 2017 年 3 月 19 日
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 件のコメント
Guillaume
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
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 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by