0

I want to create a 3d array where basically the content is identical to the indeces used to access it. So m[2,5] would result in array([2, 5]). I couldn't find an obvious solution with the numpy functions indices, ogrid, concatenate, etc.

At the moment I'm using this, but was wondering whether there is a solution that makes better use of the API:

a, b = 3, 4
m = np.ones((a, b, 2))
for x in range(a):
    m[x,:, 1] = np.array(range(b))
for y in range(b):
    m[:,y,0] = np.array(range(a))

1 Answer 1

1

Try np.mgrid:

a, b = 3, 4
m = np.mgrid[:a,:b].transpose(1,2,0)

print(m[1,2])
# array([1, 2])
Sign up to request clarification or add additional context in comments.

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.