fdlm
fdlm

Reputation: 614

Summing values of numpy array based on indices in other array

Assume I have the following arrays:

N = 8
M = 4

a = np.zeros(M)
b = np.random.randint(M, size=N) # contains indices for a
c = np.random.rand(N) # contains random values

I want to sum the values of c according to the indices provided in b, and store them in a. Writing a loop for this is trivial:

for i, v in enumerate(b):
    a[v] += c[i]

Since N can get quite big in my real-world problem I'd like to avoid using python loops, but I can't figure out how to write it as a numpy-statement. Can anyone help me out?

Ok, here some example values:

In [27]: b
Out[27]: array([0, 1, 2, 0, 2, 3, 1, 1])

In [28]: c
Out[28]: 
array([ 0.15517108,  0.84717734,  0.86019899,  0.62413489,  0.24357903,
        0.86015187,  0.85813481,  0.7071174 ])

In [30]: a
Out[30]: array([ 0.77930596,  2.41242955,  1.10377802,  0.86015187])

Upvotes: 3

Views: 1819

Answers (1)

eumiro
eumiro

Reputation: 212825

import numpy as np

N = 8
M = 4
b = np.array([0, 1, 2, 0, 2, 3, 1, 1])
c = np.array([ 0.15517108,  0.84717734,  0.86019899,  0.62413489,  0.24357903, 0.86015187,  0.85813481,  0.7071174 ])

a = ((np.mgrid[:M,:N] == b)[0] * c).sum(axis=1)

returns

array([ 0.77930597,  2.41242955,  1.10377802,  0.86015187])

Upvotes: 3

Related Questions