Discrete indexing for a loop

1 次查看(过去 30 天)
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