2
  position_array = np.array([[1,2]
                      [0,1]
                      [0,2]])

  original_array = np.array([[5,6,7]
                             [7,8,2]
                             [10,6,2]])

result_i_want = np.array([[6,7]
                        [7,2]
                        [10,2]])

how to do this?

get a new array from original_array and use position_array as index ??

for example when the first line of position_array is [1,2] that mean get column 1 and column 2 from the first line of original_array

how to do this simplest way?

0

1 Answer 1

2

You want np.take_along_axis, setting axis=1:

np.take_along_axis(original_array, position_array, axis=1)

array([[ 6,  7],
       [ 7,  8],
       [10,  2]])

Or you can also use advanced indexing:

original_array[np.arange(original_array.shape[0])[:,None], position_array]

array([[ 6,  7],
       [ 7,  8],
       [10,  2]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.