spacegoliath - 7 months ago 23

Python Question

Given an array **v** and a matrix (or ndarray) **m** containing indices of that array - what is the most efficient and/or concise way to populate the matrix with the associated array values using python+numpy?

Similar to this R question but for python+numpy.

Answer

```
v[m]
```

Example:

```
import numpy as np
v = np.random.rand((100))
m = np.array([[0, 99], [1, 0]])
print(v[m])
```

Prints out (this will vary, because it's using random numbers):

```
[[ 0.21711542, 0.07093873],
[ 0.83393247, 0.2751812 ]]
```