4
$\begingroup$

I have two lists

a = {{3,1,2},{5,1,3},{4,1,1}}
b = {{4,0,3},{4,2,2},{5,0,2}}

And would like to compare each element within

If{a>b,1,0}

expecting the result

{{0,1,0},{1,0,1},{0,1,0}}

but it doesn't work, how can I get this type of comparison?

$\endgroup$
  • 1
    $\begingroup$ Strongly related $\endgroup$ – Leonid Shifrin May 18 at 18:57
  • $\begingroup$ you could use 1-UnitStep[b-a]? $\endgroup$ – kglr May 18 at 19:02
  • $\begingroup$ Of course, fixed. $\endgroup$ – Three Diag May 18 at 19:02
  • $\begingroup$ @kglr yep, that achieves the result, thanks $\endgroup$ – Three Diag May 18 at 19:05
7
$\begingroup$
Boole@Positive[a-b]

{{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}

1-UnitStep[b-a]

{{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}

$\endgroup$
3
$\begingroup$

kglr took the fast, vectorized operations. Here are some less optimal ones that you can have a look at for fun or for learning:

Boole@MapThread[Greater, {a, b}, 2]

{{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}

Boole@Apply[Greater, Transpose[{a, b}, {3, 2, 1}], {2}]

{{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}

$\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.