Discrete indexing for a loop
    5 views (last 30 days)
  
       Show older comments
    
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
0 Comments
Accepted Answer
  Joost
      
 on 16 Jun 2020
        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 Comments
More Answers (0)
See Also
Categories
				Find more on Loops and Conditional Statements in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!