# How to get complement from two matrices?

This is a special case of my question How to do nor on matrices?

     a = IdentityMatrix[3];
b = {{1, 0}, {0, 1}, {0, 0}}
MapThread[Complement[#1, #2] &, {a, b}]


Mathematica returns {{}, {}, {1}} , whereas I expect {{0}, {0}, {1}}, which is what I need.

-
{{}, {}, {1}} is what I would expect. Just replace Complement with e.g. c in MapThread and observe what are the direct arguments to Complement. – István Zachar Feb 17 '13 at 14:56
possible duplicate of How to do nor on matrices? – István Zachar Feb 17 '13 at 14:58
Yes, this is a special case of another question of mine. But that method does not work for identity matrix. – novice Feb 17 '13 at 15:00
@IstvánZachar The OP expects to get using Complement[ConstantArray[0, 10], {0}] a list of 9 zeros! So Complement based solution does not work for him as opposed to his previous question that you have noted as duplicate. – PlatoManiac Feb 17 '13 at 15:04
Please edit title so it doesn't ask for "nor": "nor" suggest the logical/bitwise operation, which may be implemented by: bitNor[{x_,y_}]:=Mod[BitNot[BitOr[x,y]],2]. – murray Feb 17 '13 at 17:00

## 2 Answers

Try the following hack but remember this is a solution specific to your question no general solution!

newComplement[listA_, listB_] := Block[{check},
check[list_, var_] := Module[{pos},
pos = Position[list, var];
If[Length@pos != 0, Drop[list, First@pos], list]
];
If[(MemberQ[listA, #] & /@ listB) /. List ->  And,Fold[check, listA, listB], listA]
];


Testing it!

MapThread[newComplement[#1, #2] &, {a, b}]


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

-
I have tested the new self-defined newComplement.It works for n-order identity matrix with the complement of any columns. Much Gratitude! – novice Feb 17 '13 at 15:55
Plato, you don't need the Function in MapThread. You can use newComplement directly. I just made that change to an older answer of yours as well. – Mr.Wizard Mar 31 '13 at 0:12

Does this do what you want?

a = IdentityMatrix[3];

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

MapThread[Complement, {a, b}] /. {} -> {0}

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

-