Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got some big lists from which i need some data (one of the big lists). To be specific, I need to divide this biglists in sublists of the same length (10000), and form a new list with the mean of the fourth column of each sublist, and the element of the third column whose correspongin pair of the second column is closest to zero.

That sounds a little weird, but since i can't explain it any other way, here's an example: Suppose i have a list which i divide into sublists that look like this


Then what i need too get from each sublist is


(Mean[{4.02, 4, 4.16}]=4.06)

EDIT With @David Carraher 's solution, plus @Mr.Wizard 's suggestion (and the use of ReadList):

timetotal := 60;srate := 10000;
T[R_] := R/.381 - (3.81*10^-3)^-1;
rules := {
    {x_, y_, z_, w_} -> {x, y, z, T[w/.0016]}
strm = OpenRead["data6.txt"];
Skip[strm, Record, 3, NullRecords -> False];
meas = ReadList[strm, Real, RecordLists -> True] /. rules;
lst = Partition[meas, srate];
     {Mean@lst[[i]][[All, 4]], 
     lst[[i]][[First@Ordering[Abs@lst[[i]][[All, 2]], 1], 3]]}, 
     {i, timetotal}];

This code executes in 8.0484603 seconds, which is a bit more than 10 seconds faster than before. Thanks people!

share|improve this question
up vote 8 down vote accepted
{Mean@sublist[[All, 4]], sublist[[Ordering[Abs@sublist[[All, 2]]][[1]], 3]]}

{4.06, 17}


Mean@sublist[[All, 4]] provides the mean of the fourth column.


Ordering[Abs@sublist[[All, 2]]][[1]] returns the row with the closest value to zero, i.e. with the smallest absolute value.


sublist[[Ordering[%, 3]] returns the element in row 2, col 3.


share|improve this answer
I would like to mention for the record that even though David answered more quickly and has a more efficient solution, that I put {mean,val} in the right order :-) +1 from me David, good job! – tjm167us Sep 1 '13 at 0:24
Good catch, tjm. – DavidC Sep 1 '13 at 0:49
Love it! As soon as i can re-write my code i'll upload as an edit here. Than you very much! – Juan Sep 1 '13 at 2:02
This is exactly the same way I'd do it, except I'd use the second argument of Ordering so as to not sort the entire second column. {Mean @ dat[[All, 4]], dat[[First @ Ordering[Abs @ dat[[All, 2]], 1], 3]]} @Juan for you too. – Mr.Wizard Sep 1 '13 at 2:20
Hmm. I wasn't aware that Ordering had a second argument. – DavidC Sep 1 '13 at 4:13
{Mean@#[[All, 4]], #[[1, 3]]} &@SortBy[sublist, Abs@#[[2]] &]
share|improve this answer

If I'm interpreting your problem correctly:


sublist = {{1, 2, 3, 4}, {1, .2, 17, 4.02}, {1, .22, 14, 4.16}};


(*Find the mean of all of the elements in column four of each sublist*)
theMean = Mean[sublist[[#, 4]] & /@ Range[Length[sublist]]];

(*Get all of the elements in column 2 of each sublist*)
secElements = sublist[[#, 2]] & /@ Range[Length[sublist]];

(*Find which sublist has the smallest column 2 element*)
smallestSubList = Flatten[Position[secElements, Min[secElements]]];

(*The answer*)
{theMean, Flatten[sublist[[smallestSubList]]][[3]]}
share|improve this answer

Your Answer


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.