0

I am searching a sorted array for the proper insertion indices of new data so that it remains sorted. Although searchsorted2d by @Divakar works great along column insertions, it just cannot work along rows. Is there a way to perform the same, yet along the rows?

The first idea that comes to mind is to adapt searchsorted2d for the desired behavior. However, that does not seem as easy as it appears. Here is my attempt at adapting it, but it still does not work when axis is set to 0.

import numpy as np

# By Divakar
# See https://stackoverflow.com/a/40588862
def searchsorted2d(a, b, axis=0):
    shape = list(a.shape)
    shape[axis] = 1
    max_num = np.maximum(a.max() - a.min(), b.max() - b.min()) + 1
    r = np.ceil(max_num) * np.arange(a.shape[1-axis]).reshape(shape)
    p = np.searchsorted((a + r).ravel(), (b + r).ravel()).reshape(b.shape)
    return p #- a.shape[axis] * np.arange(a.shape[1-axis]).reshape(shape)

axis = 0 # Operate along which axis?
n = 16   # vector size

# Initial array
a = np.random.rand(n).reshape((n, 1) if axis else (1, n))

insert_into_a = np.random.rand(n).reshape((n, 1) if axis else (1, n))
indices = searchsorted2d(a, insert_into_a, axis=axis)
a = np.insert(a, indices.ravel(), insert_into_a.ravel()).reshape(
              (n, -1) if axis else (-1, n))

assert(np.all(a == np.sort(a, axis=axis))), 'Failed :('
print('Success :)')

I expect that the assertion passes in both cases (axis = 0 and axis = 1).

Hamdi
  • 41
  • 5
  • Original `searchsorted2d` was made to work for row-wise `searchsorted-ing`. So, that was to replicate the behavior of a loopy one - `np.searchsorted(a[i],b[i])`. If you want to make it work for `np.searchsorted(a[:,i],b[:,i])`, which I am guessing is your row-wise requirement, then simply transpose the inputs before feeding to it, so - `searchsorted2d(a.T,b.T)`. Again, this is with the original implementation of `searchsorted2d`. – Divakar May 21 '19 at 07:50
  • @Divakar I have tried `searchsorted2d(a, b)`, `searchsorted2d(a, b).reshape(b.shape)`, `searchsorted2d(a.T, b.T).reshape(b.shape)`, and `searchsorted2d(a.T, b.T).T` both with the standard implementation and `axis = 0` with no luck! If you believe your idea shall work, can you please support it with a working snapshot? – Hamdi May 21 '19 at 09:00
  • Something like this - https://ideone.com/FquZ5h – Divakar May 21 '19 at 09:54
  • For the transposed version - https://ideone.com/tZJgNi – Divakar May 21 '19 at 09:56
  • @Divakar printing the indices to match the output of a loopy function is different from what I want to achieve here. I want `i = searchsorted2d(a, b)` to return the insertion indices for making a sorted array `c = np.insert(a, i, b)`. – Hamdi May 21 '19 at 20:30

0 Answers0