-1

I have two arrays A and B with one-to-one correspondence i.e. values corresponding to [0,1] in A is 10 in B, [0,2] in A is 20 in B and so on. Now I sort A to yield a new array A1. How do I generate B1 with respect to A1? The desired output is attached.

import numpy as np
A = np.array([[[0, 1],[0, 2],[1, 3],[2, 3],[2, 5],[3, 4],[3, 6],[4, 7],[5, 6],[6, 7]]])
B = np.array([[[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]]])
A1=np.sort(A,axis=1)
#A1=array([[[0, 1],[0, 2],[1, 3],[2, 3],[3,4],[2, 5],[3, 6],[5, 6],[4, 7],[6, 7]]])

The desired output is

B1=array([[[10],[20],[30],[40],[60],[50],[70],[90],[80],[100]]])
Wiz123
  • 904
  • 8
  • 13
  • 1
    This is the same as your last question, just substitute `B` for `A` in the second line of the answer i.e. `np.array(B[0][order])` – Nick Jun 26 '22 at 08:37

2 Answers2

0

You can use argsort() function and [:] operator to ahieve the desired result:

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 5],[3, 4],[4, 7],[5, 6]]])
order = A[0,:, 1].argsort()
A =np.array([B[0][order]])
print(A)
user942761
  • 38
  • 4
0

You should use argsort:

import numpy as np

A = np.array([[[0, 1],[0, 2],[1, 3],[2, 3],[2, 5],[3, 4],[3, 6],[4, 7],[5, 6],[6, 7]]])
B = np.array([[[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]]])
idxs = np.argsort(A, axis=1)

A1 = np.take_along_axis(A, idxs, axis=1)
B1 = np.take_along_axis(B, idxs, axis=1)
Ilya
  • 730
  • 4
  • 16