0

I want to remove all rows that overlap, but I cannot find a way to do it.

If

a= ([1,2],[3,4],[5,6],[7,8])
b= ([3,4],[7,8])

k = np.delete (a,b,0)

I want something like that to work

I am expecting k = ([3,4],[7,8])

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Please check the link : https://stackoverflow.com/questions/8317022/get-intersecting-rows-across-two-2d-numpy-arrays – Hetal Thaker Apr 28 '23 at 12:04

2 Answers2

0

You can do this using:

result = [row for row in a if row  in b]

print(result)
cconsta1
  • 737
  • 1
  • 6
  • 20
Chani
  • 1
  • 3
  • Welcome to SO! While your answer may be correct, it's important to leave some explanation with it so that people can understand it better – Raahim Fareed May 01 '23 at 09:08
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 01 '23 at 09:09
0

this will be separated into 2 steps. first you need to find the indices of rows in a that overlap with b. this can be done this way:

overlap_indices = np.where(np.isin(a, b).all(axis=1))[0]

[1] np.isin(a, b): returns a boolean array of the same shape as a, where each element is True if the corresponding element of a is present in b, and False otherwise, in your case:

[[False False]
[ True  True]
[False False]
[ True  True]]

[2] .all(axis=1): returns a Boolean array of size a.shape[0] indicates the rows that all it's elements are True, in your case:

[False  True False  True]

[3] np.where() : eturns the indices of True values in the boolean array obtained by the .all(axis=1), The resulting array is a tuple containing a single element :

(array([1, 3]),)

and the [0] selects the one and only element in that tuple:

[1 3]

then you can use the

k = np.delete(a, overlap_indices, axis=0)

that deletes the elements with the specified indices and returns them in k

print(k) # [[1 2] [5 6]]

sure that could be done using for loops, but this is a vectorized implementation that is much faster if the np array is large

zayn
  • 9
  • 2