[SciPy-User] MATLAB accumarray equivalent ?
Tony S Yu
tsyu80@gmail....
Wed Mar 31 09:27:10 CDT 2010
On Mar 31, 2010, at 7:20 AM, Mohammad Abdollahi wrote:
> Dera Herlad
>
> Thank you for your reply. so what i'm trying to do is to create a re-assigned spectrogram. which means that i have a 2-d matrix representing my weighted spectrogram that i want to operate on this matrix in a way that the value of each element in matrix will be reassigned based on the value of the neighbouring elemnts of it. (i.e the sum of the values of some neighbours). for that purpose i was using accumarray in matlab which is very efficient that the trivial way of using for loops that comes to mind first. and the way i used the accumarray in matlab was sth like this : (very simple example)
>
> >> subs = [1:6;6:-1:1]
>
> subs =
>
> 1 2 3 4 5 6
> 6 5 4 3 2 1
>
> >> val = 10:15
>
> val =
>
> 10 11 12 13 14 15
>
> >> accumarray(subs',val)
>
> ans =
>
> 0 0 0 0 0 10
> 0 0 0 0 11 0
> 0 0 0 12 0 0
> 0 0 13 0 0 0
> 0 14 0 0 0 0
> 15 0 0 0 0 0
Hey Mohammad,
You can reproduce the above example with the following numpy code:
>>> import numpy as np
>>>
>>> def accumarray(idx, val):
>>> num_rows, num_cols = idx.max(axis=1) + 1
>>> new_array = np.zeros((num_rows, num_cols))
>>> new_array[tuple(idx)] = val
>>> return new_array
>>>
>>> idx = np.array([np.arange(6), np.arange(6)[::-1]])
>>> val = np.arange(10,16)
>>> print accumarray(idx, val)
Your actual use of accumarray may not be satisfied by the above, but it shouldn't be too difficult to modify it to suit your needs.
Cheers,
-Tony
More information about the SciPy-User
mailing list