0

I have 2 questions related to comparing rows in numpy arrays.

1st

Suppose we have a 2-dimensional main array whose rows we want to compare with another given 1D-array:

main_2D_array = np.array([
    [1, 2],
    [3, 4],
    [3, 5],
    [4, 5]
])

secondary_1D_array = np.array([3,4])

What I want is a function that, without using loops, compares each row of the main array with the second array, and if they are exactly the same, returns True at that position, and False otherwise. In this case, for example, it should return a one-dimensional Boolean array:

np.array([False, True, False, False])

With the True value in second position since the second row of the main array coincides with the secondary array.

This way, it would be very easy to filter the rows of the first array to keep only the ones that match

2st

Generalizing the previous case, what we want now is to compare each row of the first main array with all the rows of a 2-dimensional secondary array, so that if a row of the first array matches any of the second, return True in that position.

Let's look at an example of the expected behavior:

main_2D_array = np.array([
    [1, 2],
    [3, 4],
    [3, 5],
    [4, 5]
])

secondary_2D_array = np.array([
    [15, 15],
    [12, 12],
    [1, 2],
    [4, 5]
])

The result of the function in this case should be a boolean array with the following values:

np.array([True, False, False, True])

Where the first True is because row [1,2] is also present as a row in the second array, and the last True is because [4,5] is also present in the second array.

PD: As addition, it would be great if the code was numba-compatible, since I want to include all this within a class compiled with @jitclass. But well, if it's not possible I'll try to find alternative solutions

Dani
  • 473
  • 3
  • 21
  • 1
    Do these work for you? 1st: `np.all(main_2D_array == (secondary_1D_array), axis=1)`, 2nd: `np.all(np.isin(main_2D_array, secondary_2D_array), axis=1)` – dshanahan Apr 20 '20 at 20:37
  • @dshanahan It seems to be fulfilling the request, thank you very much! The only thing is that it does not support numba since it does not recognize the "axis" argument and the np.isin function either – Dani Apr 20 '20 at 20:47

0 Answers0