Daniel Salvadori
Daniel Salvadori

Reputation: 435

How to populate a matrix of indices with array of values?

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.

Upvotes: 0

Views: 110

Answers (1)

Carsten
Carsten

Reputation: 18446

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 ]]

Upvotes: 1

Related Questions