I have a matrix of integers, phase_space of shape (n,n), where each entry represents the number of points in that location in space. I also have tw
phase_space
(n,n)
You can use pandas.DataFrame.groupby() to accumulate all moves with same coordinates in phase_space:
pandas.DataFrame.groupby()
new_phase_space + (pd.DataFrame(phase_space) .stack() .groupby([u_x.ravel(), u_y.ravel()]) .sum() .unstack(fill_value=0) .values )
Output:
array([[2., 2., 4.], [2., 0., 4.], [0., 4., 0.]])