0

If I have a matrix containing:

n =
49898
148954
224156
224602
167493
100970
50645
21352
8168
2659
806
227
55
12
3

How can I use the numbers held within 'n' to specify the number of iterations a for-loop should contain? i.e. I want the loop to do 49898 iterations before moving on to do 148954 and so forth. I have other nested for-loops which also need to advance by '1' once 49889 or 148954 etc. complete thus I can not just SUM 'n'. I know only of:

for i=n and for i=1:n styles of for-loop, neither of which solves this issue.

AnnaSchumann
  • 1,261
  • 10
  • 22

1 Answers1

5

Use a nested for-loop:

for i=1:length(n)
    for j=1:n(i)
        % do something
    end
end
Shai
  • 111,146
  • 38
  • 238
  • 371
David
  • 8,449
  • 1
  • 22
  • 32
  • 1
    It is best [not to use `i` and `j` as variable names in Matlab](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab) – Shai Sep 18 '14 at 06:31