フィルターのクリア

I want to restart the loop from starting when a specific condition is met.

24 ビュー (過去 30 日間)
Sanyam Maheshwari
Sanyam Maheshwari 2020 年 5 月 27 日
コメント済み: Geoff Hayes 2020 年 5 月 27 日
a = table2array(BRTSTestData);
c = 20;
f = 1;
s = 0;
for i = 1:5
for j = 1:6
s = s + a(i,j);
end
if s > 20
f = f+1;
a = a/f;
break
end
for k = 1:5
s = s - a(k,i);
end
Now here I want to restart the first for loop from i = 1 when s > 20 and operate f = f+1. If I use break statement it terminates the loop and starts from next condition.

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 5 月 27 日
Sanyam - perhaps use a while loop instead.
a = table2array(BRTSTestData);
c = 20;
f = 1;
s = 0;
i = 1;
while i <= 5
for j = 1:6
s = s + a(i,j);
end
if s > 20
f = f+1;
a = a/f;
i = 1; % <--- reset i to one
break;
end
for k = 1:5
s = s - a(k,i);
end
i = i + 1; % <--- increment i
end
  2 件のコメント
Sanyam Maheshwari
Sanyam Maheshwari 2020 年 5 月 27 日
編集済み: Geoff Hayes 2020 年 5 月 27 日
while using break statement it gives control to the statement that follows the end of the loop. But I want to restart the following loop once again. please guide.
a = table2array(BRTSTestData);
c = 20;
f = 1;
s = 0;
i = 1;
j = 1;
while i <=5
while j <= 5
s = s + a(i,j);
if s > 20
a = a*f;
f = f+1;
a = a/f;
j = 1;
break
end
j = j+1;
end
for k = 1:5
s = s - a(k,i);
end
i = i + 1;
end
Geoff Hayes
Geoff Hayes 2020 年 5 月 27 日
I think in the code that I posted, the break should have been a continue
a = table2array(BRTSTestData);
c = 20;
f = 1;
s = 0;
i = 1;
while i <= 5
for j = 1:6
s = s + a(i,j);
end
if s > 20
f = f+1;
a = a/f;
i = 1; % <--- reset i to one
continue; % <--- end the current iteration and continue with the next
end
for k = 1:5
s = s - a(k,i);
end
i = i + 1; % <--- increment i
end
Perhaps that is all that you need to do too in the new code that you have posted - change the break to continue.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by