How to keep count in a nested for loop?

Hi, I'm sure the answer is simple but I just can't figure it out. Let's say I have this code:
for i=1:3
for j=1:3
count=??;
end
end
count
I want the result to be count=[1 2 3 4 5 6 7 8 9]
Thanks

답변 (2개)

the cyclist
the cyclist 2015년 11월 17일

0 개 추천

There are many ways to do this. Here is one.
c = 0;
for i=1:3
for j=1:3
c=c+1;
end
end
count = 1:c

댓글 수: 1

baby
baby 2021년 9월 26일
편집: baby 2021년 9월 26일
you saved my life.

댓글을 달려면 로그인하십시오.

the cyclist
the cyclist 2015년 11월 17일

0 개 추천

Here is one way:
irange = 1:3;
jrange = 1:3;
for i=irange
for j=jrange
end
end
count = 1:(numel(irange)*numel(jrange));

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 11월 17일

편집:

2021년 9월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by