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

4 views (last 30 days)
Vert
Vert on 27 Oct 2014
Commented: dpb on 27 Oct 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 Comment
dpb
dpb on 27 Oct 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

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!