# List of lists elementwise greater/smaller than

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?

• Strongly related Commented May 18, 2019 at 18:57
• you could use 1-UnitStep[b-a]?
– kglr
Commented May 18, 2019 at 19:02
• Of course, fixed. Commented May 18, 2019 at 19:02
• @kglr yep, that achieves the result, thanks Commented May 18, 2019 at 19:05

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 took the fast, vectorized operations. Here are some less optimal ones that you can have a look at for fun or for learning:

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

a = {{3, 1, 2}, {5, 1, 3}, {4, 1, 1}};

b = {{4, 0, 3}, {4, 2, 2}, {5, 0, 2}};

Using Ramp (new in 11.0)

Ramp[a - b]

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