Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: how to sum lists of unequal length?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70402] Re: how to sum lists of unequal length?
  • From: "dkr" <dkrjeg at adelphia.net>
  • Date: Sun, 15 Oct 2006 00:20:01 -0400 (EDT)
  • References: <egq2th$84h$1@smc.vnet.net>

Arek,

A padded approach:

addLists1[s:{__List}]:=
    Module[{len=Max[Length/@s]},Plus[Sequence@@(PadRight[#,len]&/@s)]];

A non-padded approach:

g[x_List,y_List]:=Module[{lenx=Length[x],leny=Length[y]},

Switch[Sign[lenx-leny],0,x+y,1,Join[Take[x,leny]+y,Drop[x,leny]],_,
        Join[Take[y,lenx]+x,Drop[y,lenx]]
        ]];
addLists2[s:{__List}]:=Fold[g[##]&,{0},s];

dkr


  • Prev by Date: For Routine to Map Routine
  • Next by Date: RE: Demostration
  • Previous by thread: Re: how to sum lists of unequal length?
  • Next by thread: Re: how to sum lists of unequal length?