Sparse Matrix Division

  • Follow


Hi,

I have a sparse matrix of the order of around 40000X40000 or more, and I want to normalize it so that the row sum for all rows become one.

Essentially I want to divide each row of the sparse matrix by some number (which varies for each row).

How to do this without resorting to any loops(which turns out to be time-expensive).

gautam 
0
Reply paigautam (2) 7/12/2012 6:09:08 PM

"Gautam Pai" <paigautam@ee.iisc.ernet.in> wrote in message <jtn3s3$cn6$1@newscl01ah.mathworks.com>...
> Hi,
> 
> I have a sparse matrix of the order of around 40000X40000 or more, and I want to normalize it so that the row sum for all rows become one.
> 
> Essentially I want to divide each row of the sparse matrix by some number (which varies for each row).
> 
> How to do this without resorting to any loops(which turns out to be time-expensive).
> 
> gautam 

A matrix multiply with a sparse diagonal matrix would be simple.

If you are using sparse matrices, it is time to start learning how
to avoid using loops.

John
0
Reply woodchips (7921) 7/12/2012 6:19:13 PM


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <jtn4f1$f2m$1@newscl01ah.mathworks.com>...
> "Gautam Pai" <paigautam@ee.iisc.ernet.in> wrote in message <jtn3s3$cn6$1@newscl01ah.mathworks.com>...
> > Hi,
> > 
> > I have a sparse matrix of the order of around 40000X40000 or more, and I want to normalize it so that the row sum for all rows become one.
> > 
> > Essentially I want to divide each row of the sparse matrix by some number (which varies for each row).
> > 
> > How to do this without resorting to any loops(which turns out to be time-expensive).
> > 
> > gautam 
> 
> A matrix multiply with a sparse diagonal matrix would be simple.
> 
> If you are using sparse matrices, it is time to start learning how
> to avoid using loops.
> 
> John

Hey thanks a lot John, the operation is indeed much faster using matrix multiplication.

gautam
0
Reply paigautam (2) 7/12/2012 6:48:07 PM

"Gautam Pai" <paigautam@ee.iisc.ernet.in> wrote in message <jtn3s3$cn6$1@newscl01ah.mathworks.com>...
> Hi,
> 
> I have a sparse matrix of the order of around 40000X40000 or more, and I want to normalize it so that the row sum for all rows become one.
> 
> Essentially I want to divide each row of the sparse matrix by some number (which varies for each row).
> 
> How to do this without resorting to any loops(which turns out to be time-expensive).
=================

There's also bsxfun, 

bsxfun(@rdivide,A,sum(A,2));

but for sparse matrices, it will probably be outperformed by diagonal matrix pre-multiplication.
0
Reply mattjacREMOVE (3177) 7/12/2012 6:54:13 PM

Oh, and be careful of 0/0s, especially for sparse matrices where the sparsity could be ruined by this

v=sum(A,2);
v(v==0)=1;

A=bsxfun(@rdivide,A,v);
0
Reply mattjacREMOVE (3177) 7/12/2012 6:56:13 PM

4 Replies
42 Views

(page loaded in 0.149 seconds)


Reply: