'Rearranging numpy arrays

I was not able to find a duplicate of my question, unfortunately, although I am sure that this is a problem which has been solved before

I have a numpy array with a certain set of indices, eg.

ind1 = np.array([1, 3, 5, 7])

With these indices, I can filter some values from another array. Lets call this other array rows. As an example, I can retrieve

rows[ind1] = [1, 10, 20, 15]

The order of rows[ind1] must not be changed in the following.

I have another index array, ind2

ind2 = np.array([4, 5, 6, 7])

I also have an array cols, where I can filter values from using ind2. I know that cols[ind2] results in an array which has the size of rows[ind1] and the entries are the same, but the order is different. An example:

cols[ind2] = [15, 20, 10, 1]

I would like to rearrange the order of cols[ind2], so that it corresponds to rows[ind1]. I am interested in the corresponding order of ind2.

In the example, the result should be

cols[ind2] = [1, 10, 20, 15] ind2 = [7, 6, 5, 4]

Using numpy, I did not find a way to do this. Any ideas would be helpful. Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source