This question could sound pretty silly but I can't find a way to apply element -wise tests to a list.
For example if I digit
{0.6, 1.2}>1
{{0.6,1.2},{5,0.1}}>1
I would expect to obtain
{False,True}
{{False,True},{True, False}}
respectively, but it is not the case.
Of course I can define a function or using Map
, but I can't believe there is not a core function providing this kind of result. Thank you for any indication
Answer
To my knowledge, there aren't built-in versions for comparison operators that would be automatically threaded over lists. One reason for that is that Mathematica is a symbolic system, and every auto-simplification has a cost, because there may be cases when this isn't desirable.
It is relatively easy however to construct the behavior you want:
ClearAll[l];
l[f_] := Function[Null, f[##], Listable]
Now, you can call:
{{0.6, 1.2}, {5, 0.1}} ~ l[Greater] ~ 1
(* {{False, True}, {True, False}} *)
and similarly with other comparison operations.
Note that, since you didn't mention efficiency, I intentionally left this aspect out. If you have large numerical lists, there are vastly more efficient ways to perform the comparisons, making use of vectorization and packed arrays.
Comments
Post a Comment