Easiest way to create a binary variable

Boole@Thread[Greater[x, y]] ==  MapThread[Boole@Greater@## &, {x, y}]

Inner[Greater, x, y, Boole @ {##} &]
Inner[Boole@Greater@## &, x, y, List]
Inner[Composition[Boole, Greater], x, y, List]
Boole @ Inner[Greater, x, y, List]

Update: Timings

ClearAll[r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, 
 t1, t2, t3, t4, t5, t6, t7,  t8, t9, t10, 
 f1, f2, f3, f4, f5, f6, f7,  f8, f9, f10, functions, results, timings]
f1 = Inner[Greater, ##, Boole@{##} &] &;
f2 = Inner[Boole@Greater@## &, ##, List] &;
f3 = Inner[Composition[Boole, Greater], ##, List] &;
f4 = Boole@Inner[Greater, ##, List] &;
f5 = Boole[#1 > #2 & @@@ Transpose[{##}]] &;
f6 = Transpose[{##}] /. {x_, y_} :> Boole[x > y] &;
f7 = Boole@Thread[Greater[##]] &;
f8 = MapThread[Boole@Greater@## &, {##}] &;
f9 = 1 - UnitStep[#2 - #] &;
f10[x_, y_] := Subtract[1, UnitStep@Subtract[y, x]];

functions = {"f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10"};
results = {r1, r2, r3, r4, r5, r6, r7, r8,r9, r10};
timings = {t1, t2, t3, t4, t5, t6, t7, t8, t9, t10};


SeedRandom[1]
x = Range[1000000];
y = Flatten[RandomInteger[{1, 100}, {1000000, 1}]]; 
(# = First[AbsoluteTiming[(#2 = ToExpression[#3][x, y]);]]) & @@@ 
 Transpose[{timings, results, functions}];

Equal @@ results

True

Grid[Prepend[SortBy[Transpose[{functions, ToExpression /@ functions, timings}], 
 Last], {"name", "function", "timing"}], Dividers -> All]

enter image description here

Tags:

Programming