Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

Numpy index argsorted with integer array while retaining sorting order

$
0
0

I have an array x and the result of argsorting it i. I need to sort x (and y, irrelevant here) according to i hundreds of times. It is therefore not doable to sort the data twice, everything needs to be achieved via the initial sorting i.If I take x[i] it returns the sorted x as expected. However, I now only want to use certain rows of x via n. So x[n] returns the values of x as expected.My problem is that I need to sort these x[n] via i (and will have to do the same for y[n].

# Example datax = np.array([14, 15,  9,  6, 19, 18,  4, 11, 10,  0])i = np.argsort(x)n = np.array([2, 5, 7, 8])#x[n] -> array([ 9, 18, 11, 10])

Desired output: index_sort(x, n, i) = array([ 9, 10, 11, 18])

Some simple (failed) attempts:x[n][i] -> Indexing error, as x is now too small.
x[i[n]] -> array([ 6, 11, 15, 18]), Is sorted, but contains the wrong data
x[i][n] -> Same


Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>