Discrete indexing for a loop

4 ビュー (過去 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 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