# [SciPy-user] Sparse matrix: division by vector

Tue Apr 29 21:04:03 CDT 2008

```Does this work for row sums?  For example, assuming A is in CSR and rowSum[i] stores the i-th row sum can you do

> A.data /= rowSum[A.indices]

Dinesh

--------------------------------------------------------------------------------
From: Nathan Bell <wnbell <at> gmail.com>
Subject: Re: Sparse matrix: division by vector
Newsgroups: gmane.comp.python.scientific.user
Date: 2008-04-29 18:16:19 GMT (7 hours and 44 minutes ago)

On Tue, Apr 29, 2008 at 12:16 PM, Dinesh B Vadhia
>
>
> Sparse matrix A is in csr_matrix format and I want to divide each column
> element j of A by A's column j sum (where colSum is a numpy vector) ie.
>
> > A[:,j] = A[:,j]/colSum[j]
>
> What is the most efficient way to achieve this apart from brute force ie.
>
> > A[i,j] = A[i,j]/colSum[j] ?
>
> I can initially create A in a different c**_matrix method if that helps but
> the final A has to be in csr_matrix form.
>

Assuing A is in CSR and colSum[j] stores the j-th column sum you can do

A.data /= colSum[A.indices]

--
Nathan Bell wnbell <at> gmail.com
http://graphics.cs.uiuc.edu/~wnbell/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080429/e184c44e/attachment.html
```