[R] vectorizing an iterative process.

hadley wickham h.wickham at gmail.com
Tue Dec 26 17:04:37 CET 2006


> > x[i]=3Dx[i-1]+y[i-1] for all i, how can I do this without a loop?
>
> It looks like
>    x <- cumsum(y)
>
> What does 3D mean?

The =3D is probably an encoding error - it should just be =.

In general to vectorise an iterative problem, you will need to solve
the recurrence relation
(http://en.wikipedia.org/wiki/Recurrence_relation), although I think
it's pretty tricky in general, and there's no guarantee that the
vectorised/non-recursive form will be more efficient.

Hadley



More information about the R-help mailing list