Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Vert
Vert am 27 Okt. 2014
Kommentiert: dpb am 27 Okt. 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 Kommentar
dpb
dpb am 27 Okt. 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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by