Why does my code only print the final iteration?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Guillaume
le 19 Mar 2017
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
le 19 Mar 2017
Modifié(e) : Image Analyst
le 19 Mar 2017
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.

Réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!