for loop won't stop using break

1 回表示 (過去 30 日間)
Yanxin Liu
Yanxin Liu 2021 年 2 月 4 日
コメント済み: Yanxin Liu 2021 年 5 月 9 日
I'm using ARMA to simulate and predict a series of data. "i" represens the number of pre-sample for ARMA estimate and "j" represents the number of estimation sample for ARMA estimate.
The absolute percentage errors (APE) are calculated for different numbers of pre-sample and estimate sample data.
I want to stop the loop when the APE is small than 0.05. I used break function, but it won't stop until it finishes all the loops (till i=60, j=60)
Mdl = arima(1,0,0);
APE = zeros(60,108);
for i = 12:60
for j = i:120-i
EstMdl = estimate(Mdl,Y_out((i+1):(i+j)),'Y0',Y_out(1:i));
gasforecast = forecast(EstMdl,180-i-j,'Y0',Y_out((i+1):(i+j)));
APE(i,j) = abs(gasforecast(end)-Y_out(end))/Y_out(end);
if APE(i,j) < 0.05
break;
end
end
end

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 2 月 4 日
Mdl = arima(1,0,0);
APE = zeros(60,108);
stopping = false;
for i = 12:60
for j = i:120-i
EstMdl = estimate(Mdl,Y_out((i+1):(i+j)),'Y0',Y_out(1:i));
gasforecast = forecast(EstMdl,180-i-j,'Y0',Y_out((i+1):(i+j)));
APE(i,j) = abs(gasforecast(end)-Y_out(end))/Y_out(end);
if APE(i,j) < 0.05
stopping = true;
break;
end
end
if stopping; break; end
end
  1 件のコメント
Yanxin Liu
Yanxin Liu 2021 年 5 月 9 日
I got it. Since I have an outter loop, the 'break' only stops the execution of the inner loop. Thank you so much.

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

カテゴリ

Help Center および File ExchangeConditional Mean Models についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by