Mathematica 9 is now available
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: generalized foldlist problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69085] Re: [mg69057] generalized foldlist problem
  • From: Adriano Pascoletti <pascolet at dimi.uniud.it>
  • Date: Wed, 30 Aug 2006 06:32:08 -0400 (EDT)
  • References: <200608290725.DAA28981@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 29 ago 2006, at 09:25, Arkadiusz Majka wrote:

> DearAll,
>
> Please, help!
>
> I have two list
>
> list1={a,b,c,d,e}
> list2={3,2,5,1,6}
>
> and I want to apply a modified version of FoldList to list1 in the
> following way: list2 indicates that element a appears only 3 times (if
> space enough) beginning from the beginning of the list , element b
> appears 2 times, c - 5 times , etc.
>
> So the output should be
>
> GeneralizedFoldList[list1,list2]={a,a+b,a+b+c,c+d,c+e}
>
> Thanks for any hints,
>
> arek
>

As the index i runs from 1 to the length of list1 generate
a table with list2[[i]] copies of list1[[i]] prepended with
i-1 0's, and right padded (with 0's) to the length of list1:

In[1]:=
list1 = {a, b, c, d, e};
list2 = {3, 2, 5, 1, 6};
(PadRight[Join[Table[0, {#1 - 1}], Table[list1[[#1]],
       {list2[[#1]]}]], Length[list1]] & ) /@
   Range[Length[list1]]

Out[3]=
{{a, a, a, 0, 0}, {0, b, b, 0, 0}, {0, 0, c, c, c},
   {0, 0, 0, d, 0}, {0, 0, 0, 0, e}}

and add along columns

In[4]:=
Table[1, {Length[list1]}] .
   (PadRight[Join[Table[0, {#1 - 1}], Table[list1[[#1]],
        {list2[[#1]]}]], Length[list1]] & ) /@
    Range[Length[list1]]

Out[4]=
{a, a + b, a + b + c, c + d, c + e}

Hope it helps
Adriano Pascoletti
  


  • Prev by Date: RE: using FindRoot to find multiple answers in a domain?
  • Next by Date: Re: Writing and reading a comma delimited or tab delimited file one record at a time
  • Previous by thread: generalized foldlist problem
  • Next by thread: Re: generalized foldlist problem