COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Sparse Matrix Division

• Email
• 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

See related articles to this posting

```"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 (7942) 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 (3193) 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 (3193) 7/12/2012 6:56:13 PM

4 Replies
56 Views

Similar Articles

12/12/2013 10:23:16 AM
[PageSpeed]

Similar Artilces:

sparse matrix assignment -- what is this?
I am modifying someone else's m-file that uses both sparse matrices and even sparses comments(!). I have looked in Matlab's online documentation to try to answer my question with no luck. It relates to the following bit of code (constants mine): <code> I = randperm(100); Ik = I(1:20); P = sparse(10, 10); P(Ik) = 1; </code> Output is something like... P = (1,1) 1 (2,2) 1 (5,2) 1 (9,2) 1 (1,3) 1 .... My question: what exactly is going on when a sub*sequence* of a sparse matrix is assigned a given value...

mesh a sparse matrix
Hi All, Is there any trick to speed up mesh(x) where x is a large size sparse matrix? I have GPU and multi-core here. Thanks, Mathew ...

sparse matrix newbie
Hi all I am looking for a sparse matrix library for linux gcc compiler. I haven't had much experience in the numerical analysis field and was wondering if someone more experienced could recommend an option. I have come up with at least two sparse library meschach and sparse. Meschach was reviewed by IBM and seems to be quite good. Sparse is simple and seems to be the most appropriate for my purpose. Basically I am just trying to write a simple linear system solver in C, I am using gsl for some dense matrix stuff and ODE. thanks ,Fernan >From: Fernan Bolando fernanbolando@mail...

for all rows of sparse matrix
Hello, I want to call a function for all rows/columns of a sparse matrix. That is, for i=1:numRows func1(sparseMatrix(i,:)) end Typically for full matrix I use arrayfun(@func1,fullMatrix,1:numRows) and avoid for loop for better performance. However, array fun is not accepting sparse matrix. However to call a function for all rows/columns of a sparse matrix without using for loop? Ramki "Ramakrishnan" wrote in message <jp5v3h\$5qa\$1@newscl01ah.mathworks.com>... > Typically for full matrix I use arrayfun(@func1,fullMatrix,1:numRows) and avoid for loop for better perf...

Sparse matrix transpose
I've got a big (1.3GB) sparse (nnz(A)=5%*numel(A)) matrix which was created column-wise instead of row-wise and therefore needs transposing. B=A'; % or A=A'; %results in memory problems, although I got 3.5GB I tried to do it portionwise B=[]; while ~isempty(A), ind=min(500,size(S,2)); B(end+1:end+ind,:)=A(:,1:ind)'; A(:,1:ind)=[]; end but this is so slow and may result in memory problems, too. Is there any other way to do this, e.g. via a mex function? It may be done in-place, such that only Ir and Jc are changed. Thomas halbmy wrote: > > > I've got a ...

The rank of the sparse matrix
How can we get the rank of the sparse matrix. I find the function sprank but the document said sprank(S)is the Structural rank of S. What does Structural rank mean? I have one sparse matrix (10000*5000). The sum of each row is 0, the rank should be 4999. But sprank returns 5000. yunzhi cheng wrote: > > > How can we get the rank of the sparse matrix. > I find the function sprank but the document said sprank(S)is the > Structural rank of S. > What does Structural rank mean? > I have one sparse matrix (10000*5000). > The sum of each row is 0, the rank should be 4999. >...

matrix division #3 874224
I have two known vectors, K and G, of size 1-by-n, and an unkown matrix A of size n-by-n. Originally I have the following relationship: K=G*A. How can I correctly compute A? Thanx in advance, regards guido. Guido Travaglini wrote: > > > I have two known vectors, K and G, of size 1-by-n, and an unkown > matrix A of size n-by-n. Originally I have the following > relationship: K=G*A. How can I correctly compute A? Thanx in > advance, > regards guido. Unless n == 1, you can't. Period. How many unknowns are there in an nxn matrix? n^2. How many pieces of information d...

Large sparse matrix inversion
Hi, I am trying to compute the a posteriori covariance matrix of a large sparse system: CM=inv(G'*CD*G)); What I actually need is only the variance of each estimates, i.e. the diagonal of CM. The G'*G matrix is sparse but very large (up to 30000x30000 and even more). Can you suggest me an efficient way to invert a large sparse matrix or better to only compute the elements on its diagonal? Thanks for any helpful answers. Fabrizio "Fabrizio Novali" <fabrizio@mathworks.com> wrote in message <ffhsem\$car\$1@fred.mathworks.com>... > Hi, I am trying to compute the...

Sparse matrix subroutines for Fortran?
Hi I was hoping that someone could give me some recommendations. I have a very large sparse complex Hermitian matrix that I want to find the eigenvalues of. I've decided to use Arpack to find the eigenvalues and I think I've got Arpack sorted out. Now I need to find some good subroutines for sparse matrix-dense vector multiplication operations. Naturally I thought of BLAS. However, the sparse BLAS webpage seems to indicate it is only for C and C++. They give a link to an older NIST implementation. However that is also in C. The NIST webpage says something about it being callabl...

Sparse matrix library used by Matlab?
Hello, Does anybody have any idea which library is used by Matlab for operations on sparse matrices? I know LAPACK is used for dense matrices, but I was wondering if any other standard library was used for sparse ones. Thanks, B. gao_bolin@voila.fr wrote: > Hello, > > Does anybody have any idea which library is used by Matlab for > operations on sparse matrices? I know LAPACK is used for dense > matrices, but I was wondering if any other standard library was used > for sparse ones. > As far as I know, umfpack. I don't know ... may be LINPACK You're right...

error using movtoavg with sparse matrix
Hi! I need to compute moving average for a arrays and stmovavg usually works fine, but I'm having problems when dealing with sparse matrices generated by sprand. When I do: x=sprandn(N,1,d); m=tsmovavg(x,'s',a,1); ,where N is the number of elements in the array, d is the density of non zero elements in the array and a is the moving average paramenter, then I get the following error message: " Undefined function or method 'filter' for input arguments of type 'double' and attributes 'sparse row real'. Error in ==> tsmovavg...

Multithreaded and PCT sparse matrix inversions?
Since this topic will concern my work for the next year or so (and for many other people I'm sure), I'm just wondering if there's a timeline at the Mathworks for the implementation of these two (related but definitely not the same) functions? Specifically: 1) Multithreaded sparse inversion (slash, not LU or QR or etc...) AFAIK a lot of the sparse inversion code is not multithreaded nicely, even as late as 2009b (I just ran some test one again and saw only one core was being used, versus full utilization for full invserions). Now I know there's a lot of shuffling under the hood...

Solving Large sparse matrix
Hello! I'm writting a finite element program in MatLab and therefor I have to solve large sparse equations. I use the backslash operator for this task. The matrix is positive definit. During the use of the backslash operator the amount of memory is increased drastically. The storage of the matrix is about 300MB but the backslash operator needs about 3GB storage! So it is not possible to solve larger system on a local machine! What are the reasons for this and maybe which proposal are possible? Thx "Franz-Josef " <franz.falkner@uibk.ac.at> wrote in message &...

Writing in 1 line a matrix by-column division
Hello, is it possible to write the for loop in 1 line, like for the mean? X = rand(10, 3); [M, N] = size(X); X = X - ones(M, 1) * mean(X); for i=1:3; C = X(:,i); Y(:,i) = C .* (1 / std(C)); end; X = Y; TIA In article <4218c9d3\$0\$17261\$8fcfb975@news.wanadoo.fr>, Nomak <none@invalid.domain.fr> wrote: >Hello, > >is it possible to write the for loop in 1 line, like for the mean? > > X = rand(10, 3); > [M, N] = size(X); > > X = X - ones(M, 1) * mean(X); > > for i=1:3; > C = X(:,i); &...

How do I create a 128*256 sparse matrix for LDPC?
Hi, there I am reading up some LDPC source codes in C I downloaded, I wanted to check it against Matlab, however the matrix doesn't work in Matlab, and fec.ldpcenc complains the following, "The last (N-K) columns of the parity-check matrix must be invertible in GF(2)." How do I modify the matrix? It's quite large, 128*256. Or how do I find a new matrix for it? TIA! ...

suggestions about data structures for a sparse matrix class
Hello everyone, Some time ago I coded a sparse matrix class using a hashed container that mapped a std::pair<size_t,size_t> to a double (i,j representing the location in the matrix and the double the value). This worked fine, and it was quite fast, but I had problems when trying to get the matrix-matrix multiplication so I went ahead and coded a second sparse matrix class. This time I had a hashed map that mapped the index i (representing the row) to another hashed map that mapped the j index to the value. Now the new implementation takes more time of course because there are two hashin...

sparse matrix assembly without adding duplicate values
Hi I have three vectors i_row, j_col, s_val which I use for assemblying a sparse matrix. Usually the sparse command adds any elements of s that have duplicate values of i and j together. However I dont want to add up duplicate values. I just want to take the first occurence. Is there a simple way to way to remove duplicate entries. I tried to come up with something using "unique", but it didnt work. Any other ideas...? Thanks "A B" <gitsnedbutzi@hotmail.com> wrote in message <h94umi\$nrk\$1@fred.mathworks.com>... > Hi > > I have three vectors >...

Backslash taking a loong time with large, sparse matrix
I am trying to calculate x = A\B. A in my case is a large (~40,000 x 40,000), sparse, asymmetric matrix with some complex values. I have let it run for a couple of hours with no solution and need it to be much faster. What are my options for making this faster? I can send A and B to anyone if that will help. Thanks. On 7/22/2013 11:48 PM, Kapil wrote: > I am trying to calculate x = A\B. A in my case is a large (~40,000 x 40,000), sparse, >asymmetric matrix with some complex values. I have let it run for a couple of hours with >no solution and need it to be much faster. ...

Don't detect singular sparse matrix
Why in the some cases the linear system solutions using sparse matrix (with mrdivide or inv), don't is indicated that the matrix is closed to singular? By example in version 2010a: a = speye(10,10); a(1,5) = -1; a(5,1) = -1; b = randn(10,1); sprank(a); >> ans = 10 rank(full(a)); >> ans = 9 a\b; (no warning) inv(a)*b; (no warning) full(a)\b Warning: Matrix is singular to working precision. But in version 2007a: a = speye(10,10); a(1,5) = -1; a(5,1) = -1; b = randn(10,1); sprank(a); >> ans = 10 rank(full(a)); >> ans = 9 a\b; Warning: Matr...

output selected number of rows of a sparse matrix to a text file ?
Hey guys, I have to output selected number of rows of a sparse matrix (full sparse matrix) to a file? input: sparse matrix of the form 1 2 3 4 5 6 7 8 1 4 5 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 3 0 0 0 4 0 0 0 0 4 0 0 0 4 0 0 0 0 5 0 0 0 4 0 0 0 0 6 0 0 0 4 0 0 0 0 output: first 3 rows like 1 2 3 4 5 6 7 8 1 4 5 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 3 0 0 0 4 0 0 0 0 I hope you understand the requirement. generall sparse matrix gives (1,1) 4 (1,2) 5 (3,4) 4 but I need the full rows including zeros. Regards. sing...