Tuesday, 12 April 2022

Simultaneous search through two numpy arrays

For example, please assume two NumPy arrays as below:

x = array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

y = array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Now, assume I would like to find a row in x, a row in y and an identical column for both (x and y always have an equal number of columns) such that: (i) the row in x must include the earliest N consecutive zero values (ii) the row in y (with the same starting column) must have N - i consecutive values less than MR.

If we assume N=2, i=1 and MR=1, we will have the following option: the first row in x, the first row in y, and the second column (e.g., the fourth row in x, the second row in y, and the second column is another equivalent option). Using the same option given earlier, I update the above arrays as below:

x = array([[1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
           [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

y = array([[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

While satisfying the abovementioned conditions, the search would be row by row. The above search process should return a row number for array x, a row number for array y and an identical column number for both.

I would like to do such a simultaneous search in the fastest possible way. I would be happy to know your suggestions. Thank you!



from Simultaneous search through two numpy arrays

No comments:

Post a Comment