필터 지우기
필터 지우기

Doing while and for loop together

조회 수: 1 (최근 30일)
Atik Faysal
Atik Faysal 2020년 4월 18일
댓글: Atik Faysal 2020년 4월 18일
sum_Train={};
sum=0;
for j=1:10
while j <= 10
for i=1:10
sum_Train{j,1} = sum+results{j,1}(i,i);
end
end
end
here, result is a 10*1 cell. for each value of j, i shoild go from 1 to 10. When i is 10, the value of j should be incremented by 1 and i should start from 0 again. The result is stored in sum_Train which is 10*1 cell.
  댓글 수: 2
Tommy
Tommy 2020년 4월 18일
If you run the following code, you'll see that you have created an infinite loop, as j never increases inside the while loop.
for j=1:10
while j <= 10
for i=1:10
disp([i j])
end
end
end
I don't see the need for a while loop. The following should do what you described:
sum_Train={};
sum=0;
for j=1:10
for i=1:10
sum_Train{j,1} = sum+results{j,1}(i,i);
end
end
Atik Faysal
Atik Faysal 2020년 4월 18일
Thank you. I just solved it. Just leaving it here.
sum_Train={};
sum=0;
for j=1:length(results_Train)
while j <= length(results_Train)
for i=1:length(results_Train)
sum = sum+results_Train{j,1}(i,i);
end
sum_Train{j,1}=sum;
j=j+1;
sum=0;
end
end

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

답변 (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