필터 지우기
필터 지우기

Discrete indexing for a loop

조회 수: 6 (최근 30일)
Marco Forti
Marco Forti 2020년 5월 12일
답변: Joost 2020년 6월 16일
Hello! I am having a problem with discrete indexing. I want to run the code below for the 4 values of T specified in the matrix, but what the code actually does is run it 80 times with T=0 where I did not specify a value. This slows down my computation extremely because the real code includes some other operation (I am running a Monte Carlo experiment). Is there a way to do the loop ONLY with the 4 values of T specified? Thank you in advance!
clear
clc
rng('default')
runs = 10000;
alpha_0 = -0.8;
w(1) = 0;
for T = 10*2.^(0:3)
for i = 1:runs
for t = 1:T
if t > 1
epsilon(t) = normrnd(0,1);
w(t) = alpha_0 * w(t-1) + epsilon(t);
end
end
X = w(1:T-1);
Y = w(2:T);
alpha(i) = inv(X*X')*X*Y';
zed(i) = sqrt(T)*(alpha(i)-alpha_0);
lambda(i) = inv(T)*(epsilon*epsilon');
test(i) = (alpha(i) - alpha_0)* inv(T);
qz = quantile(zed, [0.005 0.01 0.025 0.05 0.10 0.25 0.50 0.75 0.90 0.95 0.975, 0.99, 0.995]);
qt = quantile(zed, [0.005 0.01 0.025 0.05 0.10 0.25 0.50 0.75 0.90 0.95 0.975, 0.99, 0.995]);
end
alphaa(:,T) = alpha';
zeda(:,T) = zed';
lambdaa(:,T) = lambda';
test(:,T) = t;
end

채택된 답변

Joost
Joost 2020년 6월 16일
Hello,
I think you can achieve this by changing the outer for loop into:
TRange =10*2.^[0:3]
for j=1:numel(TRange)
T = TRange(j)
and keep the rest unchanged.
best regards, Joost

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