How to use "while loop" in this case?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Armando MAROZZI
el 20 de Mayo de 2021
Respondida: Scott MacKenzie
el 20 de Mayo de 2021
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 comentarios
Stephen23
el 20 de Mayo de 2021
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.
Respuesta aceptada
Más respuestas (1)
Scott MacKenzie
el 20 de Mayo de 2021
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
0 comentarios
Ver también
Categorías
Más información sobre Financial Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!