5

With a list of defined matrices

list={a,b,c,d}

How can I apply Complement to obtain

Complement[list,{b,c}]=={a,d}
Phillip Dukes
  • 938
  • 5
  • 18

3 Answers3

8

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}]]]
halirutan
  • 112,764
  • 7
  • 263
  • 474
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\}$

kglr
  • 394,356
  • 18
  • 477
  • 896
1

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}
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
James
  • 11
  • 1