I'm trying to figure out how to operate on lists of collected data using Mathematica.
I have a list x={....list of data...}
n would be the index of the list or sample number.
How do I make a function that performs something basic, y(n)=x(n)-x(n-4)?
Here is some info on the performance of the different approaches:
Note, that for the last two methods I only used AboluteTiming and the last but one only ran on 100000 instead of 10000000 data points. Also, not all methods give the exact same output.
Cheers,
Marco
This one might work, too.
Differences[Range[10], 1, 4]
Wow!, Thanks for the help, this is helping me learn Mathematica better!
I like MovingMap
MovingMap[#[[4]] - #[[1]] &, {a, b, c, d, e, f, g}, 3]
{-a + d, -b + e, -c + f, -d + g}
The example y(n) = x(n) - x(n-4) can also be computed like this:
RecurrenceFilter[{{1}, {1, 0, 0, 0, -1}}, (* list of numbers *)]
For example:
In[1]:= RecurrenceFilter[{{1},{1,0,0,0,-1}},Range[10]] Out[1] = {1,2,3,4,4,4,4,4,4,4}
Also, if a list of results is what you really want, here are two more methods:
In[1]:= x = Range[1, 10]; In[2]:= Table[x[[n]] - x[[n - 4]], {n, 5, Length[x]}] Out[2]= {4, 4, 4, 4, 4, 4} In[3]:= ListCorrelate[{-1, 0, 0, 0, 1}, x] Out[3]= {4, 4, 4, 4, 4, 4}
You can use RotateRight or RotateLeft
l1 = CharacterRange["a", "z"] n =4; l2 = RotateRight[l1, n] l1 + l2
Here I choose N = 4. You did not specify what should happen at very beginning and at the very end of the list --> here the list is wrapped around. If thi sis not what you want you have to play with the options or just cut off parts of the list
Try something like this:
list = RandomInteger[1, 10]; y[n_, list_] := Piecewise[{{list[[n]] - list[[n - 4]], n >= 5 && n <= Length[list]}, {0, n < 5 || n > Length[list]}}]; Map[y[#, list] &, Range[5, Length[list]]] Map[y[#, list] &, {5, 6, 7, 8, 9, 10}]
You'll see that the output from the last two lines is equal. Also note that lists in Mathematica start at position 1, and that list[[0]] is just the Head called List
Head
List