# Apply Complement to a list of matrices

With a list of defined matrices

list={a,b,c,d}


How can I apply Complement to obtain

Complement[list,{b,c}]=={a,d}


I guess your main confusion is that when you do

RandomSeed[1234];
list = RandomReal[1, {4, 2, 2}]
Complement[list, list[[{2, 3}]]]


then the output of the matrices is in the wrong order. The documentation of Complement says

The list returned by Complement is sorted into standard order.

An easy trick around this is to do the complement on the indices and not on the matrices itself. After that, you can use the indices to access the matrices in list:

list[[Complement[Range[Length[list]], {2, 3}]]]


You can also get the unsorted complement using DeleteCases[list, Alternatives @@ {b, c}]].

RandomSeed[777];
list = RandomInteger[10, {5, 2, 2}];
MatrixForm /@ list // TeXForm


$\left\{\left( \begin{array}{cc} 10 & 1 \\ 10 & 4 \\ \end{array} \right),\left( \begin{array}{cc} 10 & 7 \\ 6 & 6 \\ \end{array} \right),\left( \begin{array}{cc} 8 & 2 \\ 4 & 5 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 3 \\ 8 & 8 \\ \end{array} \right),\left( \begin{array}{cc} 9 & 3 \\ 1 & 10 \\ \end{array} \right)\right\}$

comp = DeleteCases[list, Alternatives @@ list[[{2, 4}]]];
comp // MatrixForm // TeXForm


$\left\{\left( \begin{array}{cc} 10 & 1 \\ 10 & 4 \\ \end{array} \right),\left( \begin{array}{cc} 8 & 2 \\ 4 & 5 \\ \end{array} \right),\left( \begin{array}{cc} 9 & 3 \\ 1 & 10 \\ \end{array} \right)\right\}$

Something I have done:

Use something like Hold to block how Complement searches arrays. In this case MatrixForm might make sense too.

For example:

Complement[Hold /@ list, Hold /@ {b, c}] ==  Hold /@ {a,d}