Chris Parry Chris Parry - 22 days ago 6
Python Question

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. Thanks!

Answer

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')
Comments