I would like define a function g[data,bins,x]
which is a piecewise constant function taking value data[[i]]
in the range bins[[i]][[1]] < x ≤ bins[[i]][[2]]
. The difficulty is that I'd like data
and bins
to be lists that are input by the user. Mathematica seems to throw errors because the argument of Piecewise
doesn't appear to be a list of pairs.
Here's an example with explicit values that works:
f[x_] :=
Evaluate@Piecewise[
Map[
{#[[1]], #[[2]][[1]] < x <= #[[2]][[2]] } &,
Transpose[{{1, 2, 3}, {{0, 1}, {1, 2}, {2, 3}}}]
]
]
However, when I try to define a general function:
g[data_, bins_, x_] :=
Evaluate@Piecewise[
Map[
{#[[1]], #[[2]][[1]] < x <= #[[2]][[2]] } &,
Transpose[{data, bins}]
]
]
I get several errors because Mathematica doesn't know if {data,bins}
can be transposed. Any help would be appreciated!