3
$\begingroup$

I have the following list:

l1 = {x y, s t, a b}

From this list, I am trying to essentially generate a new list that looks like this:

    l1 = {x==1 && y==1 && s == 0 && t == 0 && a == 0 && b == 0
|| x == 0 && y == 0 && s == 1 && t == 1 && a == 0 && b == 0
|| x == 0 && y == 0 && s == 0 && t == 0 && a == 1 && b == 1}

I.e., only one pair at a time can take on the value of 1 in each OR statement.

$\endgroup$
4
$\begingroup$
l1 = {x y, s t, a b}; 

res1 = {Or @@ 
  Inner[Sequence @@ Thread[Equal[List @@ #2, #]] &, IdentityMatrix[Length @ l1], l1, And]}

{(x == 1 && y == 1 && s == 0 && t == 0 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 1 && t == 1 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 0 && t == 0 && a == 1 && b == 1)}

Alternatively,

l2 = Flatten[l1 /. Times -> List]

{x, y, s, t, a, b}

res2 = {Or @@ 
  Inner[Equal[#2, #] &, Riffle[#, #] & /@ IdentityMatrix[Length[l2]/2], l2,  And]}

{(x == 1 && y == 1 && s == 0 && t == 0 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 1 && t == 1 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 0 && t == 0 && a == 1 && b == 1)}

$\endgroup$
  • $\begingroup$ Hi @kglr . I tried this solution, and it works great. The only problem I am having is that it doesn't work exactly as expected because your l1 is slightly different then mine. In your l1, you have l1 = {x,y,s,t,a,b}, but I have l1 = {x y, s t, a b}, i.e., each element in the list contains a product. $\endgroup$ – Thomas Moore Oct 24 at 19:21
  • 1
    $\begingroup$ @ThomasMoore, you can use l0 = {x y, s t, a b}; l1 = Flatten[l0 /. Times -> List]. I will post an alternative approach using {x y, s t, a b} directly. $\endgroup$ – kglr Oct 24 at 19:24
  • $\begingroup$ Hi @kglr Thank you very much. It is much appreciated! $\endgroup$ – Thomas Moore Oct 24 at 19:24

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.