Let's say I have a 2D NumPy array:
x = np.random.rand(100, 100000)
And I retrieve the column-wise sorted indices (i.e., each column is sorted independently from the others and the indices are returned):
idx = np.argsort(x, axis=0)
Then, for each column, I need the values from indices = [10, 20, 30, 40, 50] to be first the first 5 rows (of that column) and then followed by the rest of the sorted values (not the indices!).
A naive approach might be:
indices = np.array([10, 20, 30, 40, 50])
out = np.empty(x.shape, dtype=int64)
for col in range(x.shape[1]):
# For each column, fill the first few rows with `indices`
out[:indices.shape[0], col] = x[indices, col] # Note that we want the values, not the indices
# Then fill the rest of the rows in this column with the remaining sorted values excluding `indices`
n = indices.shape[0]
for row in range(indices.shape[0], x.shape[0]):
if idx[row, col] not in indices:
out[n, col] = x[row, col] # Again, note that we want the value, not the index
n += 1