1
$\begingroup$

What I want to do is to take a list of vectors:

u={{1,0,0,0},{0,1,0,0},{0,0,1,0}}
v={{1,1,1,1},{1,0,0,0},{0,1,0,0}}

I want to find the duplicates between u and v and then delete them from u. I am not sure why my method is not working

duplicates= Intersection[u,v]
u'=DeleteCases[u,duplicates]

But this is not working. I am not sure if it has something to do with the structure of the list, but when I use Flatten[u,1] (and for v) it gives me just a 12 component vector instead of three 4 component vectors

$\endgroup$

1 Answer 1

2
$\begingroup$

Since V 13.1 there is DeleteElements

 u = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}};
 v = {{1, 1, 1, 1}, {1, 0, 0, 0}, {0, 1, 0, 0}};

DeleteElements[u, v]

{{0, 0, 1, 0}}

Alternatively you can use

DeleteCases[Alternatives @@ v] @ u

{{0, 0, 1, 0}}

If sort order isn't important it's simply

Complement[u, v]

{{0, 0, 1, 0}}

To see this difference:

u = {{2, 2, 2, 2}, {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}};

DeleteElements[u, v]

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

But

Complement[u, v]

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.