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

2 次查看(过去 30 天)
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 个评论
dpb
dpb 2014-10-27
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

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by