Re: New User - Programming

• To: mathgroup at smc.vnet.net
• Subject: [mg52555] Re: [mg52507] New User - Programming
• From: DrBob <drbob at bigfoot.com>
• Date: Wed, 1 Dec 2004 05:59:33 -0500 (EST)
• References: <200411301024.FAA01340@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```First, here's a cleaner solution for the original cumulative sum:

a = {5, 6, 3, 2, 8, 2};
b = {5, 4, 1, 3, 9, 4};
signal = Sign[a - b];
cumulative = Rest@FoldList[Plus, 0, signal];
table := TableForm[{a, b,
signal, cumulative}, TableDirections -> Row, TableHeadings -> {{"a", "b",
"Signal", "Cumulative"}, None}, TableSpacing -> {4, 4, 4, 4, 4}]
table

Next, here's a solution for the revised cumulative sum, leaving the signal column as is:

limit = 1;
delimit = Max[-limit, Min[limit, #]] &;
cumulative = Rest@FoldList[delimit[#1 + #2] &, 0, signal];
table

You seemed to say you want to modify the SIGNAL, however, not just the cumulative sums. In that case you can calculate signal the original way, calculate cumulative, then recalculate signal:

signal = Sign[a - b];
cumulative = Rest@FoldList[delimit[#1 + #2] &, 0, signal];
signal = FoldList[#2 - #1 &, First@cumulative, Rest@cumulative];
table

Or you can compute the modified signal and cumulative simultanously, something like this:

helper[{oldS_, oldC_}, newS_] := {newS, delimit[oldC + newS]}
{signal, cumulative} = Transpose@Rest@FoldList[helper, {0, 0}, Sign[a - b]];
table

or

helper[{oldS_, oldC_}, newS_] := {Sign@newS, delimit[oldC + Sign@newS]}
{signal, cumulative} = Transpose@Rest@FoldList[helper, {0, 0}, a - b];
table

You could find a way to do this without computing a-b first, but it probably wouldn't be an improvement.

In all cases, "delimit" could be replaced with Sign if limit==1, but the more general version is needed otherwise. For instance, the last solution becomes:

helper[{oldS_, oldC_}, newS_] := {Sign@newS, Sign[oldC + Sign@newS]}
{signal, cumulative} = Transpose@Rest@FoldList[helper, {0, 0}, a - b];
table

Bobby

On Tue, 30 Nov 2004 05:24:13 -0500 (EST), Dean Williams <deanwilliams at mac.com> wrote:

> I am a new user who is well and truly stuck. Any help would be greatly
> appreciated.
>
> I have two lists, a and b, that when compared, will produce a third
> list of signals. This third list is then summed to get a cumulative
> total of the signals.
>
> The rules are fairly simple. If a = b, the signal is 0, if a > b, the
> signal is 1 and finally, if a < b, then the signal is â??1.
>
>
> a={5,6,3,2,8,2};
> b={5,4,1,3,9,4};
> c={a,b};
>
> p=0;
>
> Fun[t_,u_]/;t==u :=0;
> Fun[t_,u_]/;t>u:=1;
> Fun[t_,u_]/;t<u:=-1;
>
> signal=Map[ (Fun[#[[1]],#[[2]]])&,Transpose[c]];
>
> c4={signal};
>
> cumTotal=Flatten[Map[p+={Last[#]} &,Transpose[c4]]];
>
>
> TableForm[{a,b,signal,total},TableDirections->Row,
>     TableSpacing->{4,4,4,4,4}]
>
>
> a	b	Signal		Cum.Total
>
> 5	5	0		0
>
> 6	4	1		1
>
> 3	1	1		2
>
> 2	3	-1		1
>
> 8	9	-1		0
>
> 2	4	-1		-1
>
>
>
> I want to modify slightly how the signal is generated. The rules are
> the same except that if the cumulative total is already 1 or -1, then
> no new signal is generated unless the new signal would make the
> cumulative total move back towards zero.
>
> Ideally, I am looking to modify my code, so it produces, signal =
> {0,1,0,-1,-1,0} and cumTotal = {0,1,1,0,-1,-1}.The maximum size of the
> cumulative total need not be 1, but rather any value that is chosen as
> a limit.
>
> This has been causing me a great deal of grief and I would greatly
> welcome any help and suggestions. Given my lack of experience, I am
> sure that there is a more efficient way to tackle this type of
> problem, especially as  I am dealing with many large lists.
>
> Regards
>
> Dean Williams
>
>
>
>

--
DrBob at bigfoot.com
www.eclecticdreams.net

```

• Prev by Date: Re: New User - Programming
• Next by Date: Re: Re: pair sums applied to trignometry sums
• Previous by thread: Re: New User - Programming
• Next by thread: Re: pair sums applied to trignometry sums