Re: how to sum lists of unequal length?
- To: mathgroup at smc.vnet.net
- Subject: [mg70381] Re: [mg70352] how to sum lists of unequal length?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sun, 15 Oct 2006 00:18:43 -0400 (EDT)
- References: <200610140706.DAA07887@smc.vnet.net>
On 14 Oct 2006, at 16:06, Arkadiusz.Majka at gmail.com wrote:
> Hi all,
>
> I have lists of unknown and unequal lengths. I want to sum them, e.g
>
> {a1,a2,a3}+{b1,b2,b3,b4,b5} and obtain result
> {a1+b1,a2+b2,a3+b3,b4,b5}. Simply filling by zeros of the shorter list
> is impossible because I have no idea how many zeros I have to pad.
>
> Please, help
>
> Arek
>
One way:
AddLists[l_,m_]:=With[{
p=Max[Length[l],Length[m]]},PadRight[l,p]+PadRight[m,p]]
AddLists[{a1,a2,a3},{b1,b2,b3,b4,b5}]
{a1+b1,a2+b2,a3+b3,b4,b5}
Andrzej Kozlowski
Tokyo, Japan
- References:
- how to sum lists of unequal length?
- From: Arkadiusz.Majka@gmail.com
- how to sum lists of unequal length?