1

I have an array b containing indices of an array a. I want to insert values of another array c in the array b with same indices.

import numpy as np
a1=np.array([[1, 3, 5, 2, 3],[7, 6, 5, 2, 4],[2, 0, 5, 6, 4]])
a=a1.argsort()[:,:2]

## this will create an array with indices of 2 smallest values of a1

  a
[[0 3]
 [3 4]
 [1 0]]
b=np.array([[1],[2],[3],[4],[5],[6]])

now I want to replace value 0 in a with 1 in b ; 3 with 4 and so on

i tried using:

[a[index]]=b[index]

but its obviously not the right way as array a handles these indices as values

please help

1
  • Could you clarify a little more? You mention an array c, but don't show it in your example. Could you show what you want the output of the operation to be in your example? Commented Jun 21, 2014 at 20:59

1 Answer 1

2

If I understood you correctly, you can just use the flattened version of a to index into b:

result = b.ravel()[a.ravel()]

[1, 4, 4, 5, 2, 1]

If you need it in the same dimensions as a you can reshape it:

result = result.reshape(a.shape)

[[1, 4]
 [4, 5]
 [2, 1]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.