AnnaSchumann
AnnaSchumann

Reputation: 1271

Directly accessing matrix values within a for-loop

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.

Upvotes: 0

Views: 46

Answers (1)

David
David

Reputation: 8459

Use a nested for-loop:

for i=1:length(n)
    for j=1:n(i)
        % do something
    end
end

Upvotes: 5

Related Questions