how to repeat a block of code with a for loop?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
How do I repeat a block of code with a for loop? My code doesn't depend on the number of iterations (it isn't n^2 where n = [1 2 3 4]) instead,
I am trying to write:
loop(1) = 1;
for ii = 1:100
loop(ii) = loop(ii)+1;
_blah blah blah code_
end
1 comentario
dpb
el 27 de Oct. de 2014
Think we need more specific example of what blah... is and what it is that you're trying to repeat.
As your code snippet is written there's nothing that wouldn't work(+) but it's not clear that it's of much use, either...
(+) "Work" as in run without a compilation error. What the result would be compared to what would be wanted is something else again. Of course, one would normally preallocate loop here before the for construct as
loop=zeros(100,1); % explicitly allocate array
or equivalently,
loop(100,1)=0; % automagically allocates array
Respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!