3
$\begingroup$

Imagine I have the following matrix: X={{0,0,0},{1,0,1},{1,1,1},{1,2,1},{3,3,3}}. I want to select the sublists that are greater or equal than this list T={0,1,1}. Greater in the sense that X[[]]-T has no negative entries. The output should look like R={{1,1,1},{1,2,1},{3,3,3}}, since all elements of the sublists are equal of greater than the elements of the list T compared element by element. Thanks for help!

Thanks for the quick answers. I have searched myself meanwhile and found this one:

Higher[C_] := Select[B, NonNegative[Min[#[[]] - C[[]]]] &];

Does it seem right?

$\endgroup$
3
$\begingroup$
X = Table[RandomInteger[{-10, 10}], {30}, {3}]

1

Faster

Select[X, And @@ Thread[# > {0, 1, 1}] &]

or slower one:

Select[X, (! MemberQ[# - {0, 1, 1}, _?NonPositive]) &]
$\endgroup$
  • $\begingroup$ Thanks Kuba! It works perfectly! Which one do you recommend I use for efficiency purposes! The one you suggested or the one I found above? Thanks. :) $\endgroup$ – Friedrich Nietzsche Jun 25 '13 at 5:30
3
$\begingroup$

If x is short then it probably doesn't matter much how you do it,
but if x is long then Pick is clearly faster than either Select.
The trick is to operate on a few long vectors (the columns of x)
rather than many short vectors (the rows of x).

t = {0,1,1}; Dimensions[x = RandomInteger[3, {10^5, 3}]]
(* {100000, 3} *)

AbsoluteTiming@Dimensions@Select[x,NonNegative@Min[#-t]&]
(* {1.888830, {56520, 3}} *)

AbsoluteTiming@Dimensions@Select[x,And@@Thread[#>=t]&]
(* {2.005653, {56520, 3}} *)

AbsoluteTiming@Dimensions@Pick[x,Total@UnitStep[Transpose@x-t],3]
(* {0.275097, {56520, 3}} *)
$\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.