Message Boards Message Boards

0
|
8206 Views
|
8 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Process a list of data?

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)?

POSTED BY: Steven Stadler
8 Replies

Wow!, Thanks for the help, this is helping me learn Mathematica better!

POSTED BY: Steven Stadler

Here is some info on the performance of the different approaches:

enter image description here

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

POSTED BY: Marco Thiel

I like MovingMap

MovingMap[#[[4]] - #[[1]] &, {a, b, c, d, e, f, g}, 3]

{-a + d, -b + e, -c + f, -d + g}

POSTED BY: l van Veen

This one might work, too.

Differences[Range[10], 1, 4]

Cheers,

Marco

POSTED BY: Marco Thiel

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}
POSTED BY: Gustavo Delfino
Posted 8 years ago

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}
POSTED BY: David Keith
Posted 8 years ago

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

POSTED BY: Michael Helmle
Posted 8 years ago

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

POSTED BY: v z
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract