[Date Index]
[Thread Index]
[Author Index]
Re: New User - Programming
*To*: mathgroup at smc.vnet.net
*Subject*: [mg52535] Re: [mg52507] New User - Programming
*From*: "David Park" <djmp at earthlink.net>
*Date*: Wed, 1 Dec 2004 05:57:55 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
Dean,
This looks like a great application for functional programming in
Mathematica.
a = {5, 6, 3, 2, 8, 2};
b = {5, 4, 1, 3, 9, 4};
If you want to combine two equal length lists to make a new list, a good
command to use is Inner.
signal = Inner[Sign[#1 - #2] &, a, b, List]
{0, 1, 1, -1, -1, -1}
The Sign function is used on the difference of the elements in the a and b
lists. We want the whole operation to return a new List so that is the last
argument.
If you wanted to go with your first specification and sum all the signal
elements you could use...
Inner[Sign[#1 - #2] &, a, b, Plus]
-1
If you want to sum the signal elements but clamp the cumulative sum
between -1 and +1 then you can use another functional command, FoldList on
the signal elements,
FoldList[Max[-1, Min[1, #1 + #2]] &, 0, signal]
{0, 0, 1, 1, 0, -1, -1}
That has the starting value, 0, at the beginning of the list, which you
could get rid of by using the Drop command.
If you want to sum this new series you can Apply Plus to the intermediate
result.
Plus @@ FoldList[Max[-1, Min[1, #1 + #2]] &, 0, signal]
0
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Dean Williams [mailto:deanwilliams at mac.com]
To: mathgroup at smc.vnet.net
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,
TableHeadings->{{"a","b","Signal","Cumm.Total"},None},
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
Prev by Date:
**Inserting user material in the HelpBrowser**
Next by Date:
**Re: pair sums applied to trignometry sums**
Previous by thread:
**Re: New User - Programming**
Next by thread:
**Re: New User - Programming**
| |