# Thread: Iteration in MATLAB

1. ## Iteration in MATLAB

Hi!

Just wondering if anyone would like to help me with this.

I'd like to obtain 1000 samples each of 100 observations of the following autoregressive equation

x(t+1) = a*x(t) + e(t+1)

where x is some random variable and e is white noise. To do it once, you can write a for loop as follows:

x(1) = 0
for t = 1:100
x(t+1)=a*x(t) + e(t+1)
end

That's fine. But how would I repeat the process 1000 times, each time with a different 100 observation sample for e and also have the x series from each sample stored?

Cheers guys.

Hi!

Just wondering if anyone would like to help me with this.

I'd like to obtain 1000 samples each of 100 observations of the following autoregressive equation

x(t+1) = a*x(t) + e(t+1)

where x is some random variable and e is white noise. To do it once, you can write a for loop as follows:

x(1) = 0
for t = 1:100
x(t+1)=a*x(t) + e(t+1)
end

That's fine. But how would I repeat the process 1000 times, each time with a different 100 observation sample for e and also have the x series from each sample stored?

Cheers guys.
Code:
  sigma=1;
x=zeros(100,1000);
for t = 1:100
x(t+1,:)=a*x(t,:) + sigma*randn(1,1000);
end
RonL

3. Ron - Thank you very much for this.

May I complicate it slightly?

If x were now a vector of variables and e a vector of shocks assumed to be multivariate normal and correlated with each other, how would you obtain the 1000 samples each of 100 observations?

Ron - Thank you very much for this.

May I complicate it slightly?

If x were now a vector of variables and e a vector of shocks assumed to be multivariate normal and correlated with each other, how would you obtain the 1000 samples each of 100 observations?
Assuming that you have a function noise(N,Reps,sigma) which produces a row vector of length Reps each of who's elements is a column vector of dimension N which is multivariate normal with covariance matrix sigma:

Code:
  nt=100; nx=4; nreps=1000
sigma=[1, 0.1; 0.1, 1];
x=zeros(nt,nx,nreps);
for t = 1:nt
x(t+1,:,:)=a*x(t,:,:) + noise(4,1000,sigma);
end
RonL

5. Thanks again. People like you make the world go round.