1

if i have an array x, which value is as follow with shape (2,3,4):

array([[[ 0.15845319,  0.57808432,  0.05638804,  0.56237656],
        [ 0.73164208,  0.80562342,  0.64561066,  0.15397456],
        [ 0.34734043,  0.88063258,  0.4863103 ,  0.09881028]],

       [[ 0.35823078,  0.71260357,  0.49410944,  0.94909165],
        [ 0.02730397,  0.67890392,  0.74340148,  0.47434223],
        [ 0.02494292,  0.59827256,  0.20550867,  0.30859339]]])

and i have an index array y, which shape is (2, 3, 3), and the value is:

array([[[0, 2, 2],
        [2, 0, 2],
        [0, 0, 2]],

       [[1, 2, 1],
        [1, 1, 1],
        [1, 2, 2]]])

so i could use x[0,0,y[0][0]] to index the array x, and it will generate the output as follow:

array([ 0.15845319,  0.05638804,  0.05638804])

my question is: is there any simple way to do this? i had already tried with x[y], it did not work.

2
  • write a small function to implement the abbreviation? Commented Aug 29, 2016 at 9:19
  • i want to do it in an easy way, not a function, just look like the question i post here (stackoverflow.com/questions/39118401/…). Commented Aug 29, 2016 at 9:22

1 Answer 1

1

You could use fancy-indexing -

m,n = y.shape[:2]
out = x[np.arange(m)[:,None,None],np.arange(n)[:,None],y]
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.