[Numpy-discussion] permute and sum?
Angus McMorland
amcmorl at gmail.com
Mon Feb 28 18:50:24 EST 2011
Hi all,
I want to create a transpose of a vector, such that if the same index
is given in the 'axes' list (as per np.transpose), then the sum of the
original values sharing the same index is placed in the corresponding
output index.
For example:
In: data = np.array([5, 7, 9, 6, 2])
In: order = np.array([0, 2, 0, 3, 1])
In: permute_and_sum(data, order)
Out: array([14, 2, 7, 6])
I can obviously do this using the following,
def permute_and_sum(data, order):
result = np.zeros(np.max(order) + 1)
for val, dest in zip(data, order):
result[dest] += val
return result
but it seems like there's bound to be a much more elegant method I'm
not currently seeing. Can anyone point me in the right direction?
Thanks,
Angus.
--
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh
More information about the NumPy-Discussion
mailing list