Re: averaging sublists of different lengths
- To: mathgroup at smc.vnet.net
- Subject: [mg75485] Re: averaging sublists of different lengths
- From: Szabolcs <szhorvat at gmail.com>
- Date: Thu, 3 May 2007 03:38:06 -0400 (EDT)
- Organization: University of Bergen
- References: <f19fql$4q7$1@smc.vnet.net>
dantimatter wrote: > Hello all, > > I have a list of lists like this: > > {{70, 66, 64, 68, 64, 56, 68, 78, 62, 68, 84}, {70, 64, 64, 56, 66, > 56, 62, > 64, 66, 88, 54, 72}, {58, 54, 54, 60, 72, 70, 62, 68, 74, 76, 70}, > {66, > 56, 60, 64, 56, 62, 68, 58, 58, 58, 68, 76, 62, 76, 66, 64, 88, > 56}, {56, > 64, 72, 72, 70, 62, 76, 76, 76, 76, 86, 80, 100}, {60, 60, 70, 68, > 60, 60, > 50, 56, 60, 70, 62, 68, 88, 84, 82}, {54, 66, 72, 62, 70, 66, > 70, > 56}, {60, 60, 60, 62, 74, 80, 70}, {54, 62, 64, 72, 76, 74}, {66, > 74, 70, > 80, 54, 54, 64}, {72, 66, 60, 52, 52, 66, 66, 58, 60, 66}} > > What I'd really like is the average all the 1st values, 2nd values, > etc, but I'm having trouble figuring out how to deal with the fact > that the lists are not all the same length. Is there a way to drop > the sublists as they run out of points to add to the average, i.e. if > I want the average of all the nth values, but Length[shortSublist] < > n, can I somehow drop shortSublist and then calculate the average from > the other sublists? > This is an ugly solution, but it is simple, and if your dataset is not very large, it works works fine. In[1]:= dat = {{70, 66, 64, 68, 64, 56, 68, 78, 62, 68, 84}, {70, 64, 64, 56, 66, 56, 62, 64, 66, 88, 54, 72}, {58, 54, 54, 60, 72, 70, 62, 68, 74, 76, 70}, {66, 56, 60, 64, 56, 62, 68, 58, 58, 58, 68, 76, 62, 76, 66, 64, 88, 56}, {56, 64, 72, 72, 70, 62, 76, 76, 76, 76, 86, 80, 100}, {60, 60, 70, 68, 60, 60, 50, 56, 60, 70, 62, 68, 88, 84, 82}, {54, 66, 72, 62, 70, 66, 70, 56}, {60, 60, 60, 62, 74, 80, 70}, {54, 62, 64, 72, 76, 74}, {66, 74, 70, 80, 54, 54, 64}, {72, 66, 60, 52, 52, 66, 66, 58, 60, 66}}; In[2]:= maxLen = Max[Length /@ dat] Out[2]= 18 Pad the lists with some value that can be later removed: In[3]:= datPadded = PadRight[#, maxLen, novalue]& /@ dat; Now we have a square matrix that can be transposed: Select[#1, (# =!= novalue &) ]& /@ Transpose[datPadded] Out[4]= {{70, 70, 58, 66, 56, 60, 54, 60, 54, 66, 72}, {66, 64, 54, 56, 64, 60, 66, 60, 62, 74, 66}, {64, 64, 54, 60, 72, 70, 72, 60, 64, 70, 60}, {68, 56, 60, 64, 72, 68, 62, 62, 72, 80, 52}, {64, 66, 72, 56, 70, 60, 70, 74, 76, 54, 52}, {56, 56, 70, 62, 62, 60, 66, 80, 74, 54, 66}, {68, 62, 62, 68, 76, 50, 70, 70, 64, 66}, {78, 64, 68, 58, 76, 56, 56, 58}, {62, 66, 74, 58, 76, 60, 60}, {68, 88, 76, 58, 76, 70, 66}, {84, 54, 70, 68, 86, 62}, {72, 76, 80, 68}, {62, 100, 88}, {76, 84}, {66, 82}, {64}, {88}, {56}} In[5]:= Mean /@ % Out[5]= {686/11, 692/11, 710/11, 716/11, 714/11, 706/11, 328/5, 257/4, 456/7, 502/7, 212/3, 74, 250/3, 80, 74, 64, 88, 56} > Also, the Mean[] function requires more than one data point, but I'd > still like to extract out the values for which there is only one data > point. Is there a way to do this? > > Thanks! > dan > > Mean[] works fine with one data point: In[6]:= Mean[{1}] Out[6]= 1 Szabolcs