4
$\begingroup$

With a list of defined matrices

list={a,b,c,d}

How can I apply Complement to obtain

Complement[list,{b,c}]=={a,d}
$\endgroup$
7
$\begingroup$

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}]]]
$\endgroup$
3
$\begingroup$

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\}$

$\endgroup$

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.