[SciPy-user] Sparse matrix: division by vector

Anne Archibald peridot.faceted at gmail.com
Tue Apr 29 19:05:54 EDT 2008


On 29/04/2008, Nathan Bell <wnbell at gmail.com> wrote:
> On Tue, Apr 29, 2008 at 12:16 PM, Dinesh B Vadhia
>  <dineshbvadhia at hotmail.com> wrote:
>  >
>  >
>  > 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]

Is there any way to make the OP's approach work? Or even A /=
colsum[newaxis,:]? This seems like a basic expectation from sparse
matrices... Is there a document describing which basic numpy
operations work on sparse matrices and which don't?

Anne



More information about the SciPy-User mailing list