Is it possible for the Piecewise function to process a vector comparison? For example, my input vector is
x={0,1,2,3,4}
I would like to define a function that gives me x^2 if x<3.5, and x+3 otherwise. i.e. I would like my output to be {0,1,4,9,7}. Here I define a function:
myfunc[x_] := Piecewise[{{x^2,x<3.5},{x+3,x>=3.5}}]
which obviously does not work. Is there a way to let Mathematica know that I want to compare x with 3.5 elementwise and apply that particular value to the segment as appropriate?
I'm used to R programming where I can use the ifelse function for this job, but I'm relatively new to Mathematica and don't know if this is possible.
Thanks!
Listable
andMap
lose the speed advantage offered by vectorized operations in Mathematica. If speed is important, you may wish to use things likeUnitStep
andUnitize
instead. $\endgroup$