# 3 Lines Matlab Code to R Code (Generating autocorrelated random values)

#### consuli

##### Member
Hello,

I have found the following Matlab Code for generating autocorrelated random values, with a defined autocorrelation on lag 1.

Source: http://www.mathworks.com/matlabcentral/newsreader/view_thread/102939
I understand your question such that you want to generate
a time series whose autocorrelation series has certain
properties. The simplest way to do that, is to use
a filter and apply it to a pseudorandom sequence.

In the case of an AR(1) process, this becomes
Code:
a1=0.5;
A=[1,-a1]; # A=[1,-a1] <=> A= c(1, -a1)
x=filter(A,1,randn(1000,1)); # randn(1000,1)) <=> rnorm(1000)
But I do not know how to reproduce the Matlab Funktion filter.

Last edited:

#### Dason

##### Ambassador to the humans
We already showed you how to generate an AR(1) sequence and that is all this is doing. The parameter you set in an AR(1) sequence is the lag-1 autocorrelation.