1

I have an array as:

import numpy as np
A = np.arange(15).reshape(3, 5)

I also have an index array as:

ind = np.asarray([1,2,0,2,2])

The elements of ind represent the row number of A for each column of A.

i.e

I want to pull ind[0] = 1 element from column 0 of A I want to pull ind[4] = 2 element from column 4 of A

Desired output is:

5, 11, 2, 13, 14
1
  • 1
    a = [A[ind[x],x] for x in range(5)] Commented Oct 6, 2016 at 16:01

1 Answer 1

2

Using Numpy's fancy-indexing -

A[ind,np.arange(ind.size)]
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.