spacegoliath spacegoliath - 1 year ago 83
Python Question

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.

Answer Source


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

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

[[ 0.21711542,  0.07093873],
 [ 0.83393247,  0.2751812 ]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download