Chris Parry
Chris Parry

Reputation: 3057

Sort two numpy matrices in parallel, row by row

What is the most efficient way to sort two numpy matrices in parallel, row by row? A toy example:

sort this alpha:

a = [['c', 'b', 'e', 'd'], 
     ['a', 'd', 'b', 'e']]

then, sort this in parallel to a:

b = [['1', '2', '3', '4'], 
     ['2', '1', '4', '3']]

Result after sorting:

a = [['b', 'c', 'd', 'e'], 
     ['a', 'b', 'd', 'e']]

b = [['2', '1', '4', '3'], 
     ['2', '4', '1', '3']]

In my real case, a and b are large, 2D matrices of the same size.

If I use idx = a.argsort(), I obtain the indices to sort each row of a. Can these be applied to b in one step? b = b[idx] is not working.

Upvotes: 2

Views: 392

Answers (2)

Divakar
Divakar

Reputation: 221584

You can use advanced indexing -

idxx = np.arange(a.shape[0])[:,None],a.argsort(1)
a_out = a[idxx]
b_out = b[idxx]

Sample run -

In [75]: a
Out[75]: 
array([['b', 'c', 'd', 'e'],
       ['a', 'b', 'd', 'e']], 
      dtype='|S1')

In [76]: b
Out[76]: 
array([['2', '1', '4', '3'],
       ['2', '4', '1', '3']], 
      dtype='|S1')

In [77]: a_out
Out[77]: 
array([['b', 'c', 'd', 'e'],
       ['a', 'b', 'd', 'e']], 
      dtype='|S1')

In [78]: b_out
Out[78]: 
array([['2', '1', '4', '3'],
       ['2', '4', '1', '3']], 
      dtype='|S1')

Upvotes: 3

Rohan Khude
Rohan Khude

Reputation: 4893

Try this one

Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
    [GCC 4.8.4] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = ['c', 'b', 'e', 'd']
    >>> b = [1,    2,   3,   4 ]
    >>> a,b=zip(*sorted(zip(a, b)))
    >>> a
    ('b', 'c', 'd', 'e')
    >>> b
    (2, 1, 4, 3)
    >>> 

Upvotes: 0

Related Questions