From: ebaugh on
I am looking for an efficient way to add the elements of a list
sequentially onto another list

For example, generate list c, given lists a and b:

a = {1,2,3,4,5,6,7,8,9,10}
b = {1,2}

c = {2,4,4,5,6,7,8,9,10,11}


another example:

a = {1,1,1,1,1}
b = {1,2,3}

c = {2,3,4,2,3}

It seems like there must be a built in function to do this, but
have been unable to find it.


Thanks a 10^6
Jason Ebaugh

From: mokambo on
On Apr 22, 8:29 am, eba...(a)illinois.edu wrote:
> I am looking for an efficient way to add the elements of a list
> sequentially onto another list
>
> For example, generate list c, given lists a and b:
>
> a = {1,2,3,4,5,6,7,8,9,10}
> b = {1,2}
>
> c = {2,4,4,5,6,7,8,9,10,11}
>
> another example:
>
> a = {1,1,1,1,1}
> b = {1,2,3}
>
> c = {2,3,4,2,3}
>
> It seems like there must be a built in function to do this, but
> have been unable to find it.
>
> Thanks a 10^6
> Jason Ebaugh

One way to do it is:

c = a + PadRight[b, Length[a], b]

Alex

From: Bob Hanlon on

Your first example does not seem to be consistent with what you described.

f[a_?VectorQ, b_?VectorQ] := a + PadRight[b, Length[a], b];

a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

b = {1, 2};

f[a, b]

{2,4,4,6,6,8,8,10,10,12}

f[b, a]

{2,4}

a = {1, 1, 1, 1, 1};

b = {1, 2, 3};

f[a, b]

{2,3,4,2,3}

f[b, a]

{2,3,4}


Bob Hanlon

---- ebaugh(a)illinois.edu wrote:

=============
I am looking for an efficient way to add the elements of a list
sequentially onto another list

For example, generate list c, given lists a and b:

a = {1,2,3,4,5,6,7,8,9,10}
b = {1,2}

c = {2,4,4,5,6,7,8,9,10,11}


another example:

a = {1,1,1,1,1}
b = {1,2,3}

c = {2,3,4,2,3}

It seems like there must be a built in function to do this, but
have been unable to find it.


Thanks a 10^6
Jason Ebaugh



From: Ray Koopman on
On Apr 22, 12:29 am, eba...(a)illinois.edu wrote:
> I am looking for an efficient way to add the elements of a list
> sequentially onto another list
>
> For example, generate list c, given lists a and b:
>
> a = {1,2,3,4,5,6,7,8,9,10}
> b = {1,2}
>
> c = {2,4,4,5,6,7,8,9,10,11}
>
> another example:
>
> a = {1,1,1,1,1}
> b = {1,2,3}
>
> c = {2,3,4,2,3}
>
> It seems like there must be a built in function to do this,
> but have been unable to find it.
>
> Thanks a 10^6
> Jason Ebaugh

a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
b = {1, 2};

Total(a)PadRight@{a, b}
{2, 4, 3, 4, 5, 6, 7, 8, 9, 10}

Total(a)PadRight@{b, a}
{2, 4, 3, 4, 5, 6, 7, 8, 9, 10}