Indexing a 4D array using another array of three-dimensional indexes

A have 4D array M( a x b x c x d) and Iindex array ( 3 x f), e.g.

I = np.array([1,2,3, ...], [2,1,3, ...], [4,1,6, ...])

I would like to use Ito get a matrix Xthat has frows and columns d, where:

X[0,:] = M[1,2,4,:]
X[1,:] = M[2,1,1,:]
X[2,:] = M[3,3,6,:]
...

I know what I can use M[I[0], I[1], I[2]], but I was wondering if there is a more concise solution?

+4
source share
2 answers

You can use usage, for example:

I = np.array([[1,2,3], [2,1,3], [4,1,6]])
M = np.ndarray((10,10,10,10))
X = np.array([M[t,:] for t in I])
+1
source

This will be one way to do this -

import numpy as np

# Get row indices for use when M is reshaped to a 2D array of d-columns format
row_idx = np.sum(I*np.append(1,np.cumprod(M.shape[1:-1][::-1]))[::-1][:,None],0)

# Reshape M to d-columns 2D array and use row_idx to get final output
out = M.reshape(-1,M.shape[-1])[row_idx]

As an alternative to find row_idx, if you want to avoid np.append, you can do -

row_idx = np.sum(I[:-1]*np.cumprod(M.shape[1:-1][::-1])[::-1][:,None],0) + I[-1]

row_idx -

_,p2,p3,_ = M.shape
row_idx = np.sum(I*np.array([p3*p2,p3,1])[:,None],0)
0

All Articles