# How to use "while loop" in this case?

2 ビュー (過去 30 日間)
Armando MAROZZI 2021 年 5 月 20 日

I would like to write a loop that says: "when the number of lags is smaller than the sample size stop it".
This is what I tried:
% N.B. MidasQuantile() is from MIDAS Toolbox
% Random data
y_m = rand(300,1);
% Estimation and forecast
lags = [1:250];
estParams_m = nan(size(lags,2),9);
% Training Sample
while lags(end) < 180
for j =1:length(tau)
if j==1
[estParams_m(lags(end),1:3)] = MidasQuantile(y_m(1:180,:), Quantile',tau(j),'Period',22, 'NumLags', lags(end));
elseif j==2
[estParams_m(lags(end),4:6)] = MidasQuantile(y_m(1:180,:),'Quantile',tau(j),'Period',22, 'NumLags', lags(end));
elseif j==3
[estParams_m(lags(end),7:9)] = MidasQuantile(y_m(1:180,:),'Quantile',tau(j),'Period',22, 'NumLags', lags(end));
end
end
end
Yet, I get no results with this. Basically, I want the loop to run until the number of lags does not exceed the restricted sample size.
Can anyone help me with this?
Thanks!
##### 2 件のコメントなしを表示なしを非表示
Stephen23 2021 年 5 月 20 日
lags = [1:250]; % the square brackets are superfluous. Get rid of them.
..
while lags(end) < 180
lags(end) is 250, which is clearly greater than 180. So your WHILE loop does not even get to run one iteration.
Inside the WHILE loop you do not change lags, so even if it did iterate, your code does not change the condition.
Armando MAROZZI 2021 年 5 月 20 日
thanks for this. So, would this sort the issue?
while lags(1:end) < 180
....
end

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

### 採用された回答

Sulaymon Eshkabilov 2021 年 5 月 20 日
k=1;
while k < 180
for ..
...
end
k=k+1;
end
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Armando MAROZZI 2021 年 5 月 20 日
thanks a lot! Very helpful

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

### その他の回答 (1 件)

Scott MacKenzie 2021 年 5 月 20 日
You've got a for-loop inside a while-loop. This is likely unnescessary. Here's approximately what you need...
sampleSize = 180; % or whatever the sample size is, as per your question
for i=1:length(tau)
% do something that iterates through tau
if i == sampleSize
break; % exit the loop when you have reached the sample size
end
end
You can also set this up like this...
sampleSize = 180; % or whatever the sample size is, as per your question
i = 1;
while true
% do something (using i as index into tau, increment i afterward)
if i > samplesize
break; % exit the loop when you have reached the sample size
end
end

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

### カテゴリ

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

### Community Treasure Hunt

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

Start Hunting!

Translated by