4
$\begingroup$

There is a formula to test whether a number is included in a range? For example, if 3 is between -2 and 6.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Nov 12 '15 at 9:34
  • $\begingroup$ The key here is to understand what we mean by the range. Do you include all Real numbers between the range ? Only integers ? A start would be to have the list as a Range and use MemberQ $\endgroup$ – Lotus Nov 12 '15 at 9:39
  • $\begingroup$ There are things to do after your question is answered. Its a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please come back to do your part tomorrow. $\endgroup$ – rhermans Nov 12 '15 at 9:51
  • $\begingroup$ As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. Why not choosing a meaningful username? $\endgroup$ – rhermans Nov 12 '15 at 9:53
8
$\begingroup$
IntervalMemberQ[Interval[{-2, 6}], 3]
(* => True *)
|improve this answer|||||
$\endgroup$
8
$\begingroup$

Mathematica 10.3

In Mathematica 10.3 there is a new function Between:

Between[3, {2, 5}]
True

Mathematica graphics


Previous versions of Mathematica

You can define your own function with the same behavior:

My take

mybetween[a_, {b_, c_}] := TrueQ[b <= a <= c]

or as suggested by @mmal

mybetween[a_, {b_, c_}] := IntervalMemberQ[Interval[{b, c}], a]

We can test it:

mybetween[6, {2, 5}]
False
mybetween[3, {-2, 6}]
True
|improve this answer|||||
$\endgroup$
  • 2
    $\begingroup$ Change < to in myBetween to get the same behavior as the new built-in function. $\endgroup$ – shrx Nov 12 '15 at 9:50
  • $\begingroup$ @shrx Done, thanks for checking this. $\endgroup$ – rhermans Nov 12 '15 at 9:51
4
$\begingroup$

Have a look Range and MemberQ. As well Testing Expressions.

myList = Range[-2, 6, 1]

{-2, -1, 0, 1, 2, 3, 4, 5, 6}

myVal = 3

3

MemberQ[myList, myVal]

True

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Only works for integers though... $\endgroup$ – sebhofer Nov 12 '15 at 12:58
  • $\begingroup$ could be what he meant though.. $\endgroup$ – george2079 Nov 12 '15 at 19:02
2
$\begingroup$

You can visualize membership with

NumberLinePlot[{-2 <= x < 1 || x > 3, x == -1}, x]

enter image description here

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.