4
$\begingroup$

I have a list the form:

myList = {{0.12}, {0.12}, {0.12}, {0.12}, {0.14}, {0.12}, {0.14}, {0.12}, \
          {0.14}, {0.12}, {0.12}, {0.12}, {0.12}, {0.12}, {0.14}, {0.12}, \
          {0.14}, {0.12}, {0.14}, {0.12}, {0.12}, {0.14}, {0.12}}

How can I check if all its elements are in range .10<x<.15. It would be nice to have true or false as the answer.

With my limited knowledge,I have tried

AllTrue[myList,x_ /; `.10<x<.15`,3]

but I am not quite optimistic about it!!

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 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
    Commented Apr 16, 2015 at 15:57

5 Answers 5

4
$\begingroup$
AllTrue[Flatten@myList, .1 < # < .15 &]

(* True *)

which @kglr finds is the fastest.

$\endgroup$
1
  • $\begingroup$ Thanks David and Picket for the quick reply... $\endgroup$
    – dsingh
    Commented Apr 16, 2015 at 15:43
4
$\begingroup$
f = Through@{Min, Max}@## == {##2} &;

f[myList, .1, .15]
(* True *)

Update: Operator form of AllTrue:

AllTrue[.1 <= #[[1]] <= .15&]@myList
(* True *)

Update 2: some timings of the methods proposed so far:

functions = {"Through@{Min, Max}@## == {##2} &[list, min,max]",
"AllTrue[min<=#[[1]]<=max&]@list",
"AllTrue[min<=#<=max&]@Flatten[list]",
"AllTrue[Flatten@list, min<= # <=max &]",
"VectorQ[list, min <= First@# <= max &]",
"And @@ Flatten@IntervalMemberQ[Interval[{min, max}], list]",
"And @@ Flatten@Function[x, min <= x <=max, Listable]@list",
"And @@ MatchQ[x_ /; min <= x <=max] @@@list",
"And @@ Thread[min <= Flatten@list <=max]",
"(#[[1]] >= min && #[[-1]] <= max ) &@Sort[Flatten@list]",
"(Min[Flatten@rlst] >= min && Max[Flatten@list] <= max )"};

rlst = List /@ RandomInteger[100, 1000000];

{min,max}={0,100};
Grid[Transpose[{functions,{f[rlst, min, max] // AbsoluteTiming ,
AllTrue[min<=#[[1]]<=max&]@rlst// AbsoluteTiming,
AllTrue[min<=#<=max&]@Flatten[rlst]// AbsoluteTiming,
AllTrue[Flatten@rlst, min<= # <=max &]// AbsoluteTiming,
VectorQ[rlst, min <= First@# <= max &] // AbsoluteTiming,
And @@ Flatten@IntervalMemberQ[Interval[{min,max}], rlst]// AbsoluteTiming,
And @@ Flatten@Function[x, min <= x <=max, Listable]@rlst// AbsoluteTiming,
And @@ MatchQ[x_ /;  min <= x <=max] @@@rlst // AbsoluteTiming,
And @@ Thread[min<= Flatten@rlst <=max]// AbsoluteTiming,
(#[[1]] >= min && #[[-1]] <= max ) &@Sort[Flatten@rlst]//AbsoluteTiming,
(Min[Flatten@rlst] >= min && Max[Flatten@rlst] <= max )//AbsoluteTiming}}],
Dividers->All] 

{min,max}={0,100} enter image description here {min, max} = {0, 99} enter image description here {min, max} = {20, 70} enter image description here

$\endgroup$
3
  • $\begingroup$ one more: (#[[1]] >= min && #[[-1]] <= max ) &@Sort[Flatten@rlst] or even (Min[Flatten@rlst] >= min && Max[Flatten@rlst] <= max ) .. $\endgroup$
    – george2079
    Commented Apr 16, 2015 at 18:54
  • $\begingroup$ @george2079, I will add the two functions you suggest to the timing experiment -- it may take a day or so (i am using the free programming cloud version and i might have exceeded my daily quota today). Re Through , it is checking the {Min,Max} of Sequence[list, min, max] (not that of list; note the ##) against {min,max}. $\endgroup$
    – kglr
    Commented Apr 16, 2015 at 19:11
  • $\begingroup$ yea, I just figured that out, slick. $\endgroup$
    – george2079
    Commented Apr 16, 2015 at 19:13
2
$\begingroup$

A few alternatives

VectorQ[myList, 0.1 < First@# < 0.15 &]
(* True *)

And @@ Flatten@IntervalMemberQ[Interval[{0.1, 0.15}], myList]
(* True *)

And @@ Flatten@Function[x, 0.10 < x < 0.15, Listable]@myList
(* True *)

And @@ MatchQ[x_ /; 0.1 < x < 0.15] @@@ myList
(* True *)
$\endgroup$
2
  • $\begingroup$ Personally I like And @@ Thread[10 < myList < 15] $\endgroup$ Commented Apr 16, 2015 at 16:27
  • $\begingroup$ @2012rcampion Nice, for the OP's data you need Flatten as well And @@ Thread[0.1 < Flatten@myList < 0.15] but it's still short and concise. $\endgroup$
    – C. E.
    Commented Apr 16, 2015 at 16:37
1
$\begingroup$

Just for variety:

in[lst_, a_, b_] := 
 Min[Sign[(# - a) (b - #)] & /@lst] /. {1 -> True, _ -> 
    False}

so,

in[Flatten@myList, 0.1, 0.15]

yields True

and in[Flatten@myList, 0.02, 0.1] yields False.

Small modification for closed or half open/closed...

$\endgroup$
1
$\begingroup$

MinMax came with V 10.1 and Between with 10.3

And @@ Map[Between[{.12, .15}]] @ MinMax[myList]

True

$\endgroup$

Your Answer

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

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