1

let's say I have a 1D array a:

a = np.array([0.75,0.85,0.95,0.1])

I also have a 2x2 index array b:

b=np.array([[0,2,0,3],[1,2,1,0]])

Now I want to create a 2x2 array c, where each element in c will be chosen from array a based on the index from array b, for example, array c will look like this:

array([[0.75, 0.95, 0.75, 0.1 ],
       [0.85, 0.95, 0.85, 0.75]])

I tried to use np.take_along_axis() to achieve this but failed. Any suggestions?

2

2 Answers 2

3

a[b] ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

Sign up to request clarification or add additional context in comments.

Comments

0

another way to do it ia with a loop for or list comprehension

c = np.array([a[x] for n in b for x in n])
print(c)

output

array([0.75, 0.95, 0.75, 0.1 , 0.85, 0.95, 0.85, 0.75])

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.