Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm essentially doing coin flips, using RandomReal to populate a bunch of lists, each with 8 elements. I then want to select only those lists for which all elements are greater than a certain number, e.g. .5.

So far I have this:

class = RandomReal[1, {100, 8}];
pass = Select[class, #[[1]] > (.5) &]

but this doesn't work. I suspect a simple Select function would do the trick, but I can't seem to get the syntax right to test every element of the nested listed against a certain number and then only return those lists for which every element passes. Or indeed simply to return the number of sublists for which every element passes. Thanks.

share|improve this question
    
Select[class, And @@ Thread[# > .5] &] –  belisarius May 8 at 0:17
    
Conceptually, you want this: Select[class, And @@ Map[# > 0.5 &, #] &]. Performance-wise, you'd be better off with something like Select[class, Total[UnitStep[0.5 - #]] == 0 &]. –  Leonid Shifrin May 8 at 0:17
    
The Gods have answered so I might as well delete my answer, my answer was this: Pick[class,Times @@ UnitStep[#] == 1 & /@ (class - 0.5)] –  Pickett May 8 at 0:18
1  
@Pickett The intended effect of answering in comments isn't to preclude "real" answers, but to give some food for thought for the OP AND to other users that might want to elaborate a good post (in this case, for example doing some perf. comps, etc). Ref: meta.mathematica.stackexchange.com/q/1244/193 –  belisarius May 8 at 0:25
1  
Pick[#, Sign[(Min /@ #) - #2], 1] &[class, .5] probably about as fast as possible, change second argument obviously for limit. Properly returns those sublists where all members are greater as your OP states. –  rasher May 8 at 7:49
show 1 more comment

3 Answers 3

Maybe I'm missing something but this seems like a straightforward application of Min:

SeedRandom[1]
class = RandomReal[1, {100, 5}];

Select[class, Min[#] > 0.5 &]
{{0.823403, 0.551229, 0.746259, 0.964339, 0.89009},
 {0.745146, 0.714426, 0.809069, 0.833149, 0.725859}}
share|improve this answer
add comment

This answer summarizes the answers given in the comments to this question. I have changed two parameter values to keep the output small. Since set the values of the two parameters with With, it will to restore the original values should that be desired.

With[{size = 4}, SeedRandom @ 42; class = RandomReal[1, {100, size}]];

With[{threshold = .6}, Column@Select[class, And @@ ((# > threshold &) /@ #) &]]

With[{threshold = .6}, Column@Select[class, And @@ Thread[# > threshold] &]]

With[{threshold = .6}, Column@Select[class, Total[UnitStep[threshold - #]] == 0 &]]

With[{threshold = .6}, Column@Pick[class, Times @@ UnitStep[#] == 1 & /@ (class - threshold)]]

All of the four above expressions return

{0.717287,0.754353,0.860349,0.996966}
{0.763037,0.631343,0.89637,0.621647}
share|improve this answer
add comment

Maybe it can be done also as follows:

Select[Select[#, # > 0.5 &] & /@ class, Length[#] == 8 &]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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