Message Boards Message Boards

GROUPS:

Define a piecewise function from a list?

Posted 12 days ago
125 Views
|
4 Replies
|
2 Total Likes
|

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}
4 Replies

How about:

xlist = {1, 2, 3, 4};
ylist = {4, 5, 6};
xInterval = Interval /@ Partition[xlist, 2, 1];
f[x_] := Boole[IntervalMemberQ[#, x] & /@ xInterval] ylist

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

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.

Thank you.

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