2
$\begingroup$

How can I define a piecewise function in Mathematica?

I'm trying to define $$f_{A}(x)=\left\{\begin{aligned} 1, \quad x \in A\\ 0, \quad x\notin A \end{aligned}\right.$$ where $A\subseteq \mathbb{R}$.

$\endgroup$
1
  • 3
    $\begingroup$ Have you looked at Piecewise $\endgroup$
    – m_goldberg
    Commented Oct 23, 2020 at 15:42

1 Answer 1

4
$\begingroup$
Clear["`*"];
f[A_][x_] := Boole@MemberQ[A, x];
A = {x, y, z};
f[A][x]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.