MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Step functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg17152] RE: [mg17097] Step functions
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Sun, 18 Apr 1999 00:59:40 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Alessio Massaro wrote:
--------------------------

Can anyone help me to build a step function out of a series of {x,y} pairs?
For ex.., consider an ordered random {x,y} series like

data=Sort[Table[{Random[],Random[]}, {10}], First[#1]<First[#2]&]

I would like to build a function f[ { {x1, y1}, {x2, y2}, ...}, x ], that
evaluates to:
- y[i]        if x=x[i]
- y[i+1]    if x[i]<x<x[i+1]
Here each point {xi, yi} determines f over the left-open interval
x[i]<x<=x[i+1] of x.
The right-open interval version would be:
- y[i]        if x=x[i]
- y[i]        if x[i]<x<x[i+1]

-------------------------------------
Alessio,

My solutions below assume the (x) values are monotonic as with your data
above.

The first function: 
This is a great place to use Cases.  It's a very powerful function!  Short
of calling an executable via Math Link `LeftOpenStep` defined below may run
faster than any alternative.
 
 
LeftOpenStep[pnts_,numb_]:=
Cases[pnts,({x_,y_}/;x>=numb):>-y, 1, 1]//First
 
 
Note:
The third argument in Cases says we only want to look at level one of
(pnts).
Level one would be each ordered pair.
 
The fourth argument in Cases says we want to stop searching as soon as the
first match is found.
 
 
--------------------------
The second function:
Position can be used in very much the same way Cases was used above.  That's
what I do below.  Once I find the position of the first ordered pair where
(x>numb), I return minus one times the (y) value from the point before it.
 
 
RightOpenStep[pnts_,numb_]:=With[
  {posn=Part[Position[pnts,{x_,_}/;x>numb,1,1],1,1]},
  -data[[posn-1,2]]
]
 
 
Note:
As with Cases I use (1) as a fourth argument in Position.  This tells it to
stop searching when it finds the first match.
 
 
Regards,
Ted Ersek


  • Prev by Date: MATHEMATICA GUIDEBOOK
  • Next by Date: Data plot on banner paper
  • Previous by thread: Re: Step functions
  • Next by thread: Re: Step functions