Is there a MATLAB accumarray equivalent in numpy?

后端 未结 7 1719
时光说笑
时光说笑 2021-01-01 18:06

I\'m looking for a fast solution to MATLAB\'s accumarray in numpy. The accumarray accumulates the elements of an array which belong to the same index. An exampl

7条回答
  •  温柔的废话
    2021-01-01 18:46

    Not as good as the accepted answer but:

    [np.sum([a[x] for x in y]) for y in [list(np.where(accmap==z)) for z in np.unique(accmap).tolist()]]
    

    This takes 108us per loop (100000 loops, best of 3)

    The accepted answer (np.bincount(accmap, weights=a) takes 2.05us per loop (100000 loops, best of 3)

提交回复
热议问题