5

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?

Three Diag
  • 685
  • 4
  • 12

2 Answers2

9
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}}

kglr
  • 394,356
  • 18
  • 477
  • 896
3

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}}

C. E.
  • 70,533
  • 6
  • 140
  • 264