Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

I have two numpy arrays that represent 2D coordinates. Each row represents (x, y) pairs:

a = np.array([[1, 1], [2, 1], [3, 1], [3, 2], [3, 3], [5, 5]])
b = np.array([[1, 1], [5, 5], [3, 2]])

I would like to remove elements from a which are in b efficiently. So the result would be:

array([[2, 1], [3, 1], [3, 3]])

I can do it by looping and comparing, I was hoping I could do it easier.

share|improve this question

marked as duplicate by askewchan, Ophion, enedene, Jaime, Soner Gönül Oct 1 '13 at 14:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
This is a duplicate, I apologize, I did make a search, but was not able to find the answer. – enedene Sep 30 '13 at 21:01
5  
@enedene No worries, marking a question as a duplicate isn't an accusation or chastisement, it's really more of a pointer to a place where there are already answers. You asked the question more clearly, imo, but the answers already exist there. – askewchan Sep 30 '13 at 21:32

Python sets does a nice job of giving differences. It doesn't, though, maintain order

np.array(list(set(tuple(x) for x in a.tolist()).difference(set(tuple(x) for x in b.tolist()))))

Or to use boolean indexing, use broadcasting to create an outer equals, and sum with any and all

A = np.all((a[None,:,:]==b[:,None,:]),axis=-1)
A = np.any(A,axis=0)
a[~A,:]

Or make a and b complex:

ac = np.dot(a,[1,1j])
bc = np.dot(b,[1,1j])
A = np.any(ac==bc[:,None],axis=0)
a[~A,:]

or to use setxor1d

xx = np.setxor1d(ac,bc)
# array([ 2.+1.j,  3.+1.j,  3.+3.j])
np.array([xx.real,xx.imag],dtype=int).T
share|improve this answer

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