Re: Create a cumulative list
- To: mathgroup at smc.vnet.net
- Subject: [mg93367] Re: Create a cumulative list
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Wed, 5 Nov 2008 04:53:35 -0500 (EST)
- References: <gepb4g$sjd$1@smc.vnet.net>
philipp schrieb:
> Hi to all,
>
> I have a problem creating a program in Mathematica and I can't find
> how to do it. Actually, I found a way to find my answer, but it takes
> 59 lines ... and I need this kind of thing 4 times.
>
> So here is my problem. I have a list of values let's say ValueList =
> {1,2,3,4,5,6,7,8,9}
> What I want is Compilation = {1,3,6,10,15,21,28,36,45} created in
> adding Compilation[[1]] with ValueList[[2]] to create
> Compilation[[2]]. In the same way, Compilation[[7]] = Compilation[[6]]
> + ValueList[[7]].
>
> If that wasn't enough, I need to add a difficulty to that. If, at any
> time, a value of Compilation[[i]] is bigger that XYZ, then that
> Compilation[[i]] should be equal to 0.
>
> To help you understand (sorry but English is not my first language and
> explaining it in my first language is already tough), here is the
> results I am looking for.
>
> My ValueList = {0.0032833, -0.000654511, -0.0065494, 0.00103598,
> -0.00112899, 0.00386173, 0.00365922, 0.00551557, -0.0138527,
> 0.00471387, 0.00497326, -0.00140056, -0.00187003, -0.0195785,
> -0.00210204, -0.00900038, 0.000289855, 0.000289771, -0.00135187,
> 0.00386772}
> My Compilation = {0, -0.000654511, 0, 0.00103598, -0.0000930101, 0, 0,
> 0, 0, 0, 0, -0.00140056, 0, 0, 0, 0, 0.000289855, 0.000579626,
> -0.000772247, 0}
> and the additionnal criteria is >0.002 and <-0.002 (or >0.002 in
> absolute value)
>
> If anyone can help me please.
>
> Thanks
>
You may consider FoldList:
MyValueList = {0.0032833, -0.000654511, -0.0065494,
0.00103598, -0.00112899, 0.00386173, 0.00365922,
0.00551557, -0.0138527, 0.00471387,
0.00497326, -0.00140056, -0.00187003, -0.0195785, -0.00210204, \
-0.00900038, 0.000289855, 0.000289771, -0.00135187, 0.00386772}
ll=Rest@FoldList[Plus,0,MyValueList]
Out={0.0032833, 0.00262879, -0.00392061, -0.00288463, -0.00401362, \
-0.000151891, 0.00350733, 0.0090229, -0.0048298, -0.000115931, \
0.00485733, 0.00345677, 0.00158674, -0.0179918, -0.0200938, \
-0.0290942, -0.0288043, -0.0285146, -0.0298664, -0.0259987}
Now map the criterion:
Map[If[Abs[#]>0.002,0,#]&,ll]
Out={0, 0, 0, 0, 0, -0.000151891, 0, 0, 0, -0.000115931, 0, 0, \
0.00158674, 0, 0, 0, 0, 0, 0, 0}
Gruss Peter
--
==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de