Re: New User - Programming
- To: mathgroup at smc.vnet.net
- Subject: [mg52532] Re: [mg52507] New User - Programming
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Wed, 1 Dec 2004 05:57:52 -0500 (EST)
- References: <200411301024.FAA01340@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Nov 30, 2004, at 5:24 AM, Dean Williams 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.
>
An easier way to generate your signal is signal=Sign[a-b]
> cumTotal=Flatten[Map[p+={Last[#]} &,Transpose[c4]]];
Similarly FoldList[Plus,First[signal],Rest[signal]] will generate your
cumulative sum.
> 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.
In this case it's probably easier to work in the other direction;
compute the cumulative totals first then calculate the signal. Using
FoldList the cumulative signal can be computed with:
FoldList[Block[{s = Sign[#2[[
1]] - #2[[2]]]}, If[Abs[#1 + s] > 1, #1, #1 + s]] &,
Sign[First[a] -
First[b]], Transpose[{Rest[a], Rest[b]}]]
The signal can of course be recovered from the pairwise differences
Drop[RotateLeft[cum] - cum, -1]
where cum are the cumulative totals.
> 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.
>
The drawback to my suggestion is the formation of the temporary list
Transpose[{Rest[a],Rest[b]}], if your lists are not too large that
shouldn't be too significant a problem. Otherwise you should use a Do
or For loop.
Regards,
Ssezi