Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list of the form:

{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}}

How can I create a function which will take each set at the highest level of the list:

{{1, 2}, {3, 4}}

{{5, 6}, {7, 8}}

{{9, 10}, {11, 12}}

And subtract the first element of the set from the second element of the set, here:

{3, 4} - {1, 2} = {2, 2}

{7, 8} - {5, 6} = {2, 2}

{11, 12} - {9, 10} = {2, 2}

Then compute the average or median of these values ({2, 2} here in either case)? It's clear how to do this using loops or tables, but is there a simpler way to proceed that makes clever use of Mathematica?

For a table based implementation:

PairList = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}};
PLMean = Mean[Table[PairList[[u, 2]] - PairList[[u, 1]], {u, 1, Length[PairList]}]]
share|improve this question
1  
using your last table "form" example better just to use Part with the entire list: Mean[list[[All, 2]] - list[[All, 1]]] –  Mike Honeychurch Jul 20 '13 at 8:03
add comment

3 Answers

What about this?:

list = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}}
Mean[Subtract @@@ Reverse /@ list]

A shorter one:

Mean[Plus[-#, #2] & @@@ list]

A even shorter one:

Mean[#2 - #1 & @@@ list]
share|improve this answer
    
You calculate the Mean of a pair ("intra") individually, I think he wants the Mean of all pairs ("inter"). Maybe I am mistaken though.. –  Pinguin Dirk Jul 20 '13 at 7:41
    
@PinguinDirk Oh…Sorry for my poor English, fixed. –  xzczd Jul 20 '13 at 7:43
    
I wouldn't blame your english (I got it wrong too), but the code he added made it clear :) –  Pinguin Dirk Jul 20 '13 at 7:45
    
@xzczd Terrific, this is really nice looking. –  user8646 Jul 20 '13 at 7:46
1  
You can also use Differences instead of Subtract @@@ Reverse in this case (gives an extra {} though). –  rm -rf Jul 20 '13 at 16:58
show 3 more comments

Also this:

{-1, 1}.Mean@list

or this (looks nicer in the front end):

Mean[{-1, 1}.list\[Transpose]]

It is worth noting that the first form is considerably more time efficient than the second, though both are fast (warning, several GB RAM used):

list = RandomReal[99, {30000000, 2, 2}];

{-1, 1}.Mean@list              // Timing // First

Mean[{-1, 1}.list\[Transpose]] // Timing // First

0.1902

0.921

The second also uses about 2.5X as much memory as figured by MaxMemoryUsed[].

share|improve this answer
3  
Simon, I hate you. ;-) (I was sniffing around Dot trying to solve this puzzle and I failed. Well done!) –  Mr.Wizard Jul 20 '13 at 12:24
    
@Mr.Wizard Heh heh. For those who like compact code the standard form of Dot must surely rate as one of the best in terms of functionality per pixel. And it's infix... :-) –  Simon Woods Jul 22 '13 at 10:12
add comment

This method should be the most efficient:

Mean[#[[All, 2]] - #[[All, 1]]& @ {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}}]
{2, 2}

Another method (of many possible) is more complicated and therefore not recommended (I left it here for educational purposes): use Apply at the first level (@@@) this function #2 - #1 & (subtracting first and second elements of lists) and than map (/@) Mean:

Mean /@ Transpose[#2 - #1 & @@@ {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}}]
{2, 2}
share|improve this answer
2  
Or, just for fun, Mean[(#[[2]] - #[[1]]) &@(lst\[Transpose])] (+1) –  TomD Jul 20 '13 at 9:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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