Broadcast 1D array against 2D array for lexsort : Permutation for sorting each column independently when considering yet another vector
问题 Consider the array a np.random.seed([3,1415]) a = np.random.randint(10, size=(5, 4)) a array([[0, 2, 7, 3], [8, 7, 0, 6], [8, 6, 0, 2], [0, 4, 9, 7], [3, 2, 4, 3]]) I can create b which contains the permutation to sort each column. b = a.argsort(0) b array([[0, 0, 1, 2], [3, 4, 2, 0], [4, 3, 4, 4], [1, 2, 0, 1], [2, 1, 3, 3]]) I can sort a with b a[b, np.arange(a.shape[1])[None, :]] array([[0, 2, 0, 2], [0, 2, 0, 3], [3, 4, 4, 3], [8, 6, 7, 6], [8, 7, 9, 7]]) That was the primer to illustrate