MathGroup Archive 2012

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

Search the Archive

Re: Creating a Piecewise function with imported list of

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128958] Re: Creating a Piecewise function with imported list of
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Wed, 5 Dec 2012 03:16:38 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20121204091330.9E84468AF@smc.vnet.net>

data = {{0, 1}, {300, 2}, {600, 3}};


f[x_] = Piecewise[Thread[{
     data[[All, 2]],
     (#[[1]] <= x < #[[2]]) & /@
       Partition[
        Append[data[[All, 1]], Infinity],
        2, 1] /.
      lb_ <= x < Infinity :> x >= lb}]];


f /@ {-1, 0, 100, 300, 400, 600, 700}

{0, 1, 1, 2, 2, 3, 3}


Bob Hanlon


On Tue, Dec 4, 2012 at 4:13 AM, Nick Broderick <crazychessman at msn.com> wrot e:
> Apologies if this seems like a simple question, but I'm not familiar enough with Mathematica syntax to know how to do this. Essentially I have an arbitrary list (length n) of pairs -- taken from an input file -- from which 
I need to define a Piecewise function. The pairs (x_i,y_i) define the left-
sided endpoints for each portion of the function, and the last point define
s the value y_n of the function for x > x_n. For example:
>
> {{0,1},{300,2},{600,3}}
>
> defines a Piecewise function:
>
> f(x)= {{1, 0 <= x <300},{2, 300 <= x <600}, {3, x > 600}, {0, True}
}
>
> But I'm not sure how to go about creating a arbitrary list of conditions from my list of pairs. Any help would be appreciated.
>



  • Prev by Date: Modifying Default Stylesheet
  • Next by Date: Re: Mathematica 9: bug in merging cells
  • Previous by thread: Creating a Piecewise function with imported list of arbitrary length
  • Next by thread: Re: Creating a Piecewise function with imported list of arbitrary length