2
$\begingroup$

My question is that how can we from the old table (2 column and a bunch of rows) create a new table (2 column also) in which the first value of the new column is the difference between the second and the third value of the first old column. And the first value of the second new column is the average value of the second and third of the second old column. For example, I have the old table which is:

old={{7,9},{2,5},{3,7},{10,9}}

then the new table should be:

new={{-5,7},{1,6},{7,8}}

I would really appreciate it if you could help me out. Thank you

$\endgroup$
3
$\begingroup$

Since you want a column of differences and a column of moving averages I think it is more intuitive to simply use functions with those names:

Transpose[{Differences[old[[All, 1]]], MovingAverage[old[[All, 2]], 2]}]

{{-5, 7}, {1, 6}, {7, 8}}

$\endgroup$
1
$\begingroup$
Transpose[{Differences@#, Mean /@ Partition[#2, 2, 1]} & @@ Transpose[old]]

{{-5, 7}, {1, 6}, {7, 8}}

$\endgroup$
0
$\begingroup$

Another variant:

Transpose@ MapThread[#1@#2 &, {{Differences, MovingAverage[#, 2] &}, Transpose[old]}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.