Message Boards Message Boards

0
|
2621 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Define a piecewise function from a list?

Posted 6 years ago

I would like to define a piecewise function by providing two lists and using a for loop eg:

xlist = {1, 2, 3, 4}
ylist = {4, 5, 6}
f(x):= { ylist[1] if x in [ xlist[1],xlist[2] ];ylist[2] if x in [ xlist[2],xlist[3] ]; ylist[3] if x in [ xlist[3],xlist[4] ];  0 otherwise}
POSTED BY: Noureddine Toumi
4 Replies

Thank you.

POSTED BY: Noureddine Toumi

Sorry, my mistake. It should read (as a dot product):

f[x_] := Boole[IntervalMemberQ[#, x] & /@ xInterval].ylist

Please note that the problem is at the borders of the intervals not well defined.

POSTED BY: Henrik Schachner

Thank you this solved partially the problem but I want to have the function returning the exact value not a list.

POSTED BY: Noureddine Toumi

How about:

xlist = {1, 2, 3, 4};
ylist = {4, 5, 6};
xInterval = Interval /@ Partition[xlist, 2, 1];
f[x_] := Boole[IntervalMemberQ[#, x] & /@ xInterval] ylist
POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract