how to repeat a block of code with a for loop?

4 visualizzazioni (ultimi 30 giorni)
Vert
Vert il 27 Ott 2014
Commentato: dpb il 27 Ott 2014
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 Commento
dpb
dpb il 27 Ott 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

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by