3
$\begingroup$

Given lists of lists like a and b, I want to obtain an output like c.

a = {{2, 1, 0}, {1, 2, 1}}; b = {{2, 2, 0}, { 2, 2, 1}};  

c = {{2, 0, 0}, {0, 2, 1}}

If the same position has a different value or zero, it should output zero. What's the elegant, non-loopy way of doing this?

$\endgroup$
5
$\begingroup$

One way:

MapThread[#1 Boole[#1 == #2] &, {a, b}, 2]
$\endgroup$
8
$\begingroup$

This will be vastly faster for large lists:

BitXor[1, Unitize[Subtract[a, b]]]*a
$\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.