Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two Boolean matrices, that both have dimensions m x n. I want to create a function TableImplies, which would return True if one matrix piecewise implies the other; in other words, given matrices A = (a_ij) and B = (b_ij), then

TableImplies[A, B] = true if for all i and j, a_ij => b_ij.

For example:

truePattern = Table[True, {2}, {3}]

{{True, True, True}, {True, True, True}}

falsePattern = Table[False, {2}, {3}]

{{False, False, False}, {False, False, False}}

TableImplies[falsePattern, truePattern]


TableImplies[truePattern, falsePattern]


randomPattern = Table[RandomInteger[] == 1, {2}, {3}]

{{True, True, False}, {True, True, False}}

TableImplies[randomPattern, randomPattern]


What I got so far:

ElementWiseImplies[x_, y_] = Implies[x, y]

SetAttributes[ElementWiseImplies, Listable]

ElementWiseImplies[RandomPattern, RandomPattern]

{{True, True, True}, {True, True, True}}

Now this is all good. Next:

Apply[And, %, {0, 2}]


That seems to work. But if I do like this:

TableImplies[x_, y_] = Apply[And, ElementWiseImplies[x, y], {0, 2}]

x && y

the result is not what I wish. I understand, why this is happening: Mathematica applies And not to the result of ElementWiseImplies, but to the expression ElementWiseImplies[x, y] itself, so the result becomes And[x, y], which is not what I expected.

What should I do? Use local variable x = ElementWiseImplies[x, y]; and then Apply[And, x, {0, 2}]? Or is there more elegant solution?

share|improve this question
up vote 4 down vote accepted
tableImplies[a_, b_] := And @@ Flatten@MapThread[Implies, {a, b}, 2]


a = Table[aa[i, j], {i, 2}, {j, 2}];
b = Table[bb[i, j], {i, 2}, {j, 2}];
tableImplies[a, b]

(aa[1, 1] \[Implies] bb[1, 1]) && 
(aa[1, 2] \[Implies] bb[1, 2]) && 
(aa[2, 1] \[Implies] bb[2, 1]) && 
(aa[2, 2] \[Implies] bb[2, 2])

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.