Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct a ndarray from ndarray according to list of coordinate [duplicate]

I want to build a ndarray taget from src ndarray according to some coordinates. Here is an example

src = np.arange(12).reshape(3,4)
coordinates = [[[0,0],[0,1],[0,3]],
               [[2,1],[1,1],[0,1]]]
target = src.SOME_API(coordinates)
# expect target as
# [[0,1,3],
#  [9,5,1]]

How can I do this?

like image 424
Qinsheng Zhang Avatar asked Jan 31 '26 17:01

Qinsheng Zhang


1 Answers

You can use this tuple indexing to get values of each set of indices, and then transpose it to get your desired shape:

target = src[tuple(coordinates.T)].T

output:

[[0 1 3]
 [9 5 1]]
like image 126
Ehsan Avatar answered Feb 02 '26 08:02

Ehsan