4

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?

1
  • Your existing solution looks quite concise, doesn't it? Commented Jun 23, 2015 at 13:21

2 Answers 2

1

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])
Sign up to request clarification or add additional context in comments.

Comments

0

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)

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.