3

I have an array a of size (M, N, K). And I have an array b of size (M, N) with integer values of [0, K-1].

How do I get the array... c of size (M, N), where c[i, j] == a[i, j, b[i, j]] in the simplest manner?

Which part of the indexing guide is it?

2
  • 1
    Consider giving an example. Though this seems to be something like: a[*np.indices(b.shape).reshape(2, -1), b.ravel()].reshape(*b.shape) Commented Jan 7 at 18:58
  • @Onyambu, thanks! Thought it's a difficult way :) Not far for c = np.array([ a[i, j, b[i,j]] for i in range(M) for j in range(N) ]).reshape(M, N). Post as a separate answer? Commented Jan 7 at 21:26

2 Answers 2

3

You can use advanced indexing:

c = a[np.arange(M)[:, None], np.arange(N), b]

Output:

array([[ 0,  6, 12, 18],
       [24, 25, 31, 37],
       [43, 49, 50, 56]])

Taking @Vitalizzare's example:

# input
M, N, K = 3, 4, 5
a = np.arange(M*N*K).reshape(M, N, K)
b = np.arange(M*N).reshape(M, N) % K

# output
array([[ 0,  6, 12, 18],
       [24, 25, 31, 37],
       [43, 49, 50, 56]])
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I will take a look at the guide on... [Mx1, 1xN, K] indexing.
aha..... index broadcasting?
@Kuraga yes, here it's [Mx1, N, MxN].
Yes, MxN not K.
3

To extract data this way you pass corresponding indexes as 2-dimentional arrays in each of 3 dimensions, where the first 2 should be the result of meshgrid for matrices (note indexign='ij'):

a[*np.meshgrid(range(M), range(N), indexing='ij'), b]

See Advanced Indexing in NumPy and numpy.meshgrid for details.


Example:

import numpy as np

M, N, K = 3, 4, 5
a = np.arange(M*N*K).reshape(M, N, K)
b = np.arange(M*N).reshape(M, N) % K
first, second = np.meshgrid(range(M), range(N), indexing='ij')
c = a[first, second, b]

assert all(a[i, j, b[i,j]] == c[i,j] for i in range(M) for j in range(N))

1 Comment

Oh, thanks! I forgot about a[np.array(...), np.array(...), np.array(...)]. But thought it's a difficult way, too. Not far from c = np.array([ a[i, j, b[i,j]] for i in range(M) for j in range(N) ]).reshape(M, N)? But without loops and thanks about the reminder!

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.