# [SciPy-dev] Scipy equivalents of MATLAB's sparse/find/accumarray

Nathan Bell wnbell@gmail....
Mon Apr 7 15:39:25 CDT 2008

```On Mon, Apr 7, 2008 at 1:53 PM, Viral Shah
<vshah@interactivesupercomputing.com> wrote:
> Nathan, thanks for your earlier response on sparse matrix indexing.
>
>  The other thing that I found cumbersome with the current sparse
>  implementation in scipy is not being able to figure out the exact
>  equivalents of Matlab's sparse/accumarray and find. In the following
>  examples, I will denote Matlab codes with M>> and Python with P>>
>
>  M>> S = sparse (I, J, V, m, n)
>
>  The equivalent in Python seems to be:
>  P>> S = sparse.csc_matrix ((obj, ij), dims)
>  Or
>  P>> S = sparse.csr_matrix ((obj, ij), dims)

That's correct.  These are just wrappers for
coo_matrix((obj,ij)).tocsc() and coo_matrix((obj,ij)).tocsr().

Likewise, find() in MATLAB implemented by .tocoo().

>  It wasn't clear looking at the documentation, what happens when there
>  are duplicate row/col values. Matlab's sparse() adds duplicates, and
>  accumarray() provides a more general way to combine duplicates. I was
>  wondering, if it would be possible to add an extra argument that would
>  allow the user to specify what to do with duplicates. Options could be
>  last, any, sum, prod, max, min, argmax, argmin, or a user specified
>  function.

Currently the coo->csr and coo->csc conversions sum duplicates together.

accumarray() is probably better accomplished by argsorting the index
tuples and performing whatever transformation you wish on the permuted
values.

--
Nathan Bell wnbell@gmail.com
http://graphics.cs.uiuc.edu/~wnbell/
```

More information about the Scipy-dev mailing list