I have a matrix A
whose rows have to be reordered or shuffled according to the sequence in array b
. The actual size of a Matrix A is more than 100 rows. The suggested answer need to be scalable.
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([3,1,2])
Desired Output:
A = np.array([[7,8,9],[1,2,3],[4,5,6]])
A = A[b-1,...]
that's it. This is called 'broadcasting' arrays. Also keep in mind that arrays in python indexed from 0 (not from 1!), thus we substract -1 from b.