How to number permutation values in "for" loop to be sequentially printed as 1, 2, 3 etc?

Hi. I have
a = [1 2 3]
b = [1 2 3]
c = [1 2 3]
I am running for loop for i = 1:3, j= 1:3 and m = 1:3. I need to print the output in terms of 1, 2, 3, 4, etc, instead of 111, 112, 113, 114, etc.

 Accepted Answer

cnt = 0;
for ii = 1:3
for jj = 1:3
for kk = 1:3
cnt = cnt+1
...
end
end
end

3 Comments

Thanks Stephen for the answer and suggestion,
Can I please ask to have it in a different form. For example, when I had two sets,
a = [1 2 3]
b = [1 2 3]
I used:
k= (i-1)*3 + j
Basically, I would like to modify this to suit three sets instead of two. Thanks again.
for ii = 1:3
for jj = 1:3
for kk = 1:3
k = (ii-1)*3*3 + (jj-1)*3 + kk
...
end
end
end

Sign in to comment.

More 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!