0

I'm trying to extract from a 3D matrix of values a 2d matrix were the last dimension has values from the last dimension of the 3d matrix. For example if P of dimensions [2,2,3] =

[ [[5, 1, 5], [9, 9, 4]], [[0, 9, 8], [8, 6, 8]] ]

what is the index matrix in order to get the out matrix [[1, 9],[0, 8]] where 1 is the 2nd element of the first column of the first row, 9 is the 1st element of the first row second column, 0 is the 1st element of the second row first column and 8 is the 3rd element of the second row second column? The idea is that, for each column I have k different scores. I want to retrieve for each column a different score for which I know the index.

I'm a bit confused with advanced indexing in Numpy and I'm not figuring it out by my self. Thanks!

4
  • 1
    How are you getting that pattern of 2nd element, 1st element, 1st element & 3rd element? Or is it given as a separate indexing variable? Commented Jun 10, 2018 at 15:36
  • It can be another matrix that I use to index. The problem is given the matrix P and the matrix out what is the matrix I such that P[I] = out? I understand that I could be actually an expression for indexing or advanced indexing in numpy. What I mean is that I know which index I have to pick from P, what I don't know is how to index P in the correct way to have the right output. Commented Jun 10, 2018 at 15:39
  • @TommasoPasini that may not be easy. There a duplicate elements in P. Commented Jun 10, 2018 at 15:42
  • why is it a problem? Commented Jun 10, 2018 at 20:04

1 Answer 1

3

I am assuming there's an indexing array to index into the last axis. Let's call it idx. For the given sample with the given text in the question, it would be -

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

Specifically, this was extracted from the quoted text :

1 is the 2nd element of the first column of the first row, 9 is the 1st element of the first row second column, 0 is the 1st element of the second row first column and 8 is the 3rd element of the second row second column

To solve, we will use open grid with np.ogrid to index into the first two axes of the input array -

m,n = idx.shape
I,J = np.ogrid[:m,:n]
out = A[I,J,idx]

Sample run -

In [57]: A
Out[57]: 
array([[[5, 1, 5],
        [9, 9, 4]],

       [[0, 9, 8],
        [8, 6, 8]]])

In [59]: idx = np.array([[1,0],[0,2]])

In [60]: m,n = idx.shape

In [61]: I,J = np.ogrid[:m,:n]

In [62]: A[I,J,idx]
Out[62]: 
array([[1, 9],
       [0, 8]])
Sign up to request clarification or add additional context in comments.

1 Comment

perfect! I already tried this way before and I don't know why it didn't work probably I missed something. Thank you very much! Could you explain why as idx I need a 2 dimensional matrix? Thanks

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.