# Creating a simple function to compute the average of the difference between pairs of elements in an array

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]}]]

• 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

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]

• 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
• You can also use Differences instead of Subtract @@@ Reverse in this case (gives an extra {} though). – rm -rf Jul 20 '13 at 16:58

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[].

• 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

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}

• Or, just for fun, Mean[(#[] - #[]) &@(lst\[Transpose])] (+1) – user1066 Jul 20 '13 at 9:18