Constrain the size of the intersection

Let 2 matrices m1 and m2 of integers with size nX3.

The constraint I want to create is: m1(i,[1,2,3]) (the ith row of m1) and m2(i,[1,2,3]) (the ith row of m2) have exactly 2 elements in common.

Is there a nice clever and concrete way to express that?

I can only think of a very long and disgusting logical clause of the sort:

(m1(:,1)==m2(:,1) AND m1(:,2)==m2(:,2) AND m1(:,3)~= m2(:,3)) OR the other 5 symmetric subclauses, like the one I wrote, to cover every case.

Any suggestion?

1 Like