Indexing a 4D array using another array of 3D indices

177 views Asked by At

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

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

I would like to use I to arrive at a matrix X that has f rows and d columns, where:

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

I know I can use M[I[0], I[1], I[2]], however, I was wondering if there's a more concise solution?

2

There are 2 answers

0
Arcturus B On BEST ANSWER

You can use use, 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])
0
Divakar On

This would be one way to do it -

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 would like 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]

Or little less scary way to get row_idx -

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