Why does my code only print the final iteration?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Guillaume
el 19 de Mzo. de 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
el 19 de Mzo. de 2017
Editada: Image Analyst
el 19 de Mzo. de 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.

Respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!