I'm trying to make calculations on a list that depend on the previous value. In my case I'm doing event detection - detecting the point where a value rises above some threshold for the first time
e.g.
list[t]=SomeFunction[list[t-1] ];
A perfect general example would be calculating a fibonacci number non-recursively.
fib[0]=0
fib[1]=1
fib[n]= fib[n-1]+fib[n-2]
If my function just took the value at t
I could simply use Map
. But I need
to work with the value at t-1
.
What's the right (functional/mathematica) way to do this? I'm sure there's a way built into mathematica for this, I'm just not finding it in the documentation.
stream = {1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1};
stream[[6]] > 5 && stream[[5]] <= 5
Update: Expanding from my concrete example a bit. There are many algorithms and formula, which rely on e.g. the previous or n-1 values in their calculation.
Map is a very simple way to transform a list by a function. Is there an equivalent, elegant, method to do the same where the function may need previous (or n-1, neighbouring) values?
A perfect general example would be calculating a fibonacci number non-recursively.
fib[0]=0
fib[1]=1
fib[n]= fib[n-1]+fib[n-2]