[SciPy-User] MATLAB accumarray equivalent ?

Mohammad Abdollahi m.abdollahi@gmail....
Wed Mar 31 05:28:20 CDT 2010


thank you for your replys, but both of these methods are for the case when
the subs argument is a vector. but apparently they can not be used when subs
is a matrix. i.e. the output of the function is meant to be a matrix too.
right ?

On Fri, Mar 26, 2010 at 4:25 PM, Alan G Isaac <aisaac@american.edu> wrote:

> On 3/26/2010 10:39 AM, Mohammad Abdollahi wrote:
> > Does anyone know what is the equivalent function of MATLAB's
> > "accumarray()" in scipy/numpy,  with the exact same functionality ?
>
> That certainly gets the basic accumarray functionality:
>
>         >>> vals = 101+np.arange(5)
>         >>> subs = np.array([1,2,4,2,4])-1
>         >>> np.bincount(subs,vals)
>         array([ 101.,  206.,    0.,  208.])
>
> Here's another way that maybe easier
> to adapt to whatever the OP needs:
>
>         >>> from collections import defaultdict
>         >>> d = defaultdict(float)
>         >>> for i, cat in enumerate(subs):
>         ...   d[cat] += vals[i]
>         ...
>         >>> d
>         defaultdict(<type 'float'>, {0: 101.0, 1: 206.0, 3: 208.0})
>
> That should actually be pretty fast,
> though not as fast as bincount of course.
>
> What's the use case?
>
> hth,
> Alan Isaac
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100331/f2c809f8/attachment.html 


More information about the SciPy-User mailing list