Chuz
Chuz

Reputation: 11

Sort a Matrix by a specific column (with 2-digits numbers or more)

I want to sort a numpy matrix by the order of its first column numbers (the command have to move all the row of each element of this column), I tried to use the "argsort()" command, but it doesn't work well when the numbers are upper than 9, because it works only with the first digit of each element, for example for the following numpy matrix:

1.1 ....... ........ .......
1.2 ....... ........ .......
1.3 ....... ........ .......
2.1 ....... ........ .......
2.2 ....... ........ .......
2.3 ....... ........ .......
3.1 ....... ........ .......
3.2 ....... ........ .......
3.3 ....... ........ .......
6.1 ....... ........ .......
6.2 ....... ........ .......
6.3 ....... ........ .......
8.1 ....... ........ .......
8.2 ....... ........ .......
8.3 ....... ........ .......
5.1 ....... ........ .......
5.2 ....... ........ .......
5.3 ....... ........ .......
4.1 ....... ........ .......
4.2 ....... ........ .......
4.3 ....... ........ .......
12.1 ....... ........ .......
12.2 ....... ........ .......
12.3 ....... ........ .......
11.1 ....... ........ .......
11.2 ....... ........ .......
11.3 ....... ........ .......
10.1 ....... ........ .......
10.2 ....... ........ .......
10.3 ....... ........ .......
9.1 ....... ........ .......
9.2 ....... ........ .......
9.3 ....... ........ .......
7.1 ....... ........ .......
7.2 ....... ........ .......

cjarray.sort=cjarray[cjarray[:, 0].argsort()]

1.1 ....... ........ ....... 
1.2 ....... ........ ....... 
1.3 ....... ........ ....... 
10.1 ....... ........ ....... 
10.2 ....... ........ ....... 
10.3 ....... ........ ....... 
11.1 ....... ........ ....... 
11.2 ....... ........ ....... 
11.3 ....... ........ ....... 
12.1 ....... ........ ....... 
12.2 ....... ........ ....... 
12.3 ....... ........ ....... 
2.1 ....... ........ ....... 
2.2 ....... ........ ....... 
2.3 ....... ........ ....... 
3.1 ....... ........ ....... 
3.2 ....... ........ ....... 
3.3 ....... ........ ....... 
4.1 ....... ........ ....... 
4.2 ....... ........ ....... 
4.3 ....... ........ ....... 
5.1 ....... ........ ....... 
5.2 ....... ........ ....... 
5.3 ....... ........ ....... 
6.1 ....... ........ ....... 
6.2 ....... ........ ....... 
6.3 ....... ........ ....... 
7.1 ....... ........ ....... 
7.2 ....... ........ ....... 
7.3 ....... ........ ....... 
8.1 ....... ........ ....... 
8.2 ....... ........ ....... 
8.3 ....... ........ ....... 
9.1 ....... ........ ....... 
9.2 ....... ........ ....... 

As you can see, the matrix jumps from 1.3 to 10.1 because the command only concerned about the first digit, can somebody give a hand.

Thanks a lot!

Upvotes: 1

Views: 238

Answers (2)

user1415946
user1415946

Reputation:

This is what I used for a long time to emulate MATLAB sortrows. Tbh .argsort seems more appealing and should work just fine. It was not available a couple of years ago.

def sortrows(data):
    """assumes data to be numpy.ndarray"""
    data = np.ascontinuousarray(data)
    return np.sort(
        data.view(['', data.dtype] * data.shape[1]), axis=0
    ).view(data.dtype)

Upvotes: 0

Irshad Bhat
Irshad Bhat

Reputation: 8709

You should argsort() only that column along which you want to sort the matrix and use the generated indices to sort the matrix. Demo:

>>> a=np.array([[12,2,3,4],[22,1,3,5],[4,1,6,7],[15,1,2,7]])
>>> a
array([[12,  2,  3,  4],
       [22,  1,  3,  5],
       [ 4,  1,  6,  7],
       [15,  1,  2,  7]])
>>> a[a[:,0].argsort()]
array([[ 4,  1,  6,  7],
       [12,  2,  3,  4],
       [15,  1,  2,  7],
       [22,  1,  3,  5]])

Upvotes: 1

Related Questions