pythonarrayssortingnp.argsort

How to sort a 2D array by rows and store indices in new 2d array


I have an array of dimension (43, 25520).

I want to sort them for every of those 43 columns and then store the sorted indices for every row in a new 2d array. So the output should be a new sorted (43, 25520).

first_sorted = np.argsort(active_genevector[2,:])

With that I get the sorted 25520 indices of the first of 43 rows.

How can I do that to get in the end it for all 43 as a (43, 25520) array?


Solution

  • I made a short script to test what you need:

    import numpy as np
    
    test = np.random.random((43,25520))
    
    result = np.argsort(test, axis = 1)
    
    print(result.shape)
    

    I think this would work for you.