Is there a MATLAB accumarray equivalent in numpy?

后端 未结 7 1725
时光说笑
时光说笑 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:54

    It depends on what exactly you are trying to do, but numpy unique has a bunch of optional outputs that you can use to accumulate. If your array has several identical values, then unique will count how many of the identical values there are by setting the return_counts option to true. In some easy applications, this is all that you need to do.

    numpy.unique(ar, return_index=False, return_inverse=False, return_counts=True, axis=None)
    

    You can also set the index to true and use it to accumulate a different array.

提交回复
热议问题