[Numpy-discussion] nicest way to apply an arbitrary sequence of row deltas to an array

George Dahl george.dahl at gmail.com
Sun Dec 20 21:38:51 EST 2009


Hi everyone,
I was wondering if anyone had insight on the best way to solve the
following problem.

Suppose I have a numpy array called U.
U has shape (N,M)
Suppose further that I have another array called dU and that
dU has shape (P,M) and that P has no particular relationship to N, it
could be larger or smaller.
Additionally, I have a one dimensional array called idx and
idx has shape (P,).  Furthermore idx holds integers that are all valid
indices into the rows of U.  idx almost certainly contains some
duplicates.

I want,

for k in range(P):
   U[idx[k],:] += dU[k,:]

Is there a nice vectorized and efficient way to do this without making
the obvious python for loop I included above?  For what I am doing, P
is usually quite large.  I am most interested in a clever use of
numpy/scipy commands and Python and not Cython.


Thanks in advance for any suggestions.

- George



More information about the NumPy-Discussion mailing list