# Issue

This Content is from Stack Overflow. Question asked by deemel

I want to extract parts of an numpy ndarray based on arrays of index positions for some of the dimensions. Let me show this on an example

# Example data

``````dummy = np.random.rand(5,2,100)
X = np.array([[0,1],[4,1],[2,0]])
``````

`dummy` is the original ndarray with dimensionality 5x2x100. This dimensionality is arbitrary, it could as well be 5x2x4x100.
`X` is a matrix of index values, here `X[:,0]` are the indices of the first dimension of `dummy`, `X[:,1]` those of the second dimension. The number of columns in X is always the number of dimensions in `dummy` minus 1.

# Example output

I want to extract an ndarray of the following form for this example

``````[
dummy[0,1,:],
dummy[4,1,:],
dummy[2,0,:]
]
``````

# Complications

If the number of dimensions in `dummy` were fixed, this could just be done by `dummy[X[:,0],X[:,1],:]` . Sadly the dimensionality can be different, e.g. `dummy` could be a 5x2x4x6x100 ndarray and `X` correspondingly would then be 3×4 . My attempts at dealing with it have not yielded the desired result.

• `dummy[X,:]` yields a 3x2x2x100 ndarray for this example same as `dummy[X]`
• Iteratively reducing `dummy` by doing something like `dummy = dummy[X[:,i],:]` with `i` an iterator over the number of columns of `X` also does not reduce the ndarray in the example past 3x2x100

I have a feeling that this should be pretty simple with numpy indexing, but I guess my search for a solution was missing the right terms for this.
Does anyone have a solution to this?

# Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

```
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.```