Re: generalized foldlist problem
- To: mathgroup at smc.vnet.net
- Subject: [mg69082] Re: [mg69057] generalized foldlist problem
- From: "Christoph Lhotka" <lhotka at astro.univie.ac.at>
- Date: Wed, 30 Aug 2006 06:32:04 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi!
One prodcedural approach:
f[list1_,list2_]:=Plus@@Table[
PadRight[
Join[
Table[0,{i-1}],
Table[list1[[i]],
{list2[[i]]}]],
Length[list1]],
{i,1,Length[list1]}]
one rule based approach:
f[list1_,list2_]:=Plus@@With[{dim=Length[list1]},
SparseArray[
{{i_,j_}:>list1[[i]]/;
j<=list2[[i]]+i-1&&
j>=Position[list1,list1[[i]]][[1,1]]
},
{dim,dim}]]
Both should work for lists of same length.
wkr christoph
On Tue, 29 Aug 2006 03:25:38 -0400 (EDT)
"Arkadiusz Majka" <Arkadiusz.Majka at telekomunikacja.pl> 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
>
-- Mag. Christoph Lhotka --
University of Vienna / Institute for Astronomy
fon. +43 (1) 4277 51841
mail. lhotka at astro.univie.ac.at