Thread Subject: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?

Subject: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?

From: zedong

Date: 4 Jan, 2009 05:24:01

Message: 1 of 3

Is there any mex implementation of sparse matrix that I can add only one element to the matrix?
 I am working on finite element method.I want to use sparse matrix.But mex file has no function for sparse matrix for add only one element.
for example,I want to do the following thing in c programming:
a is now a sparse matrix of 100000 by 100000(for example)
elem is the index matrix of 10000 by 3;
for(i=0;i<10000;i++)
{
        for(j=0;j<3;j++)
        for(k=0;k<3;k++)
       {
              jdex=elem(i,j);
              kdex=elem(i,k);
               a(jdex,kdex)=a(jdex,kdex)+f( ) /*f is some function to compute the value needed to be added*/
       }
}
Do you know any use friendly code about sparse matrix in mex file that contain the interface function as above?Thank you very much

Subject: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?

From: John D'Errico

Date: 4 Jan, 2009 09:56:02

Message: 2 of 3

"zedong
" <zdongwu@gmail.com> wrote in message <gjph5h$a0o$1@fred.mathworks.com>...
> Is there any mex implementation of sparse matrix that I can add only one element to the matrix?
> I am working on finite element method.I want to use sparse matrix.But mex file has no function for sparse matrix for add only one element.
> for example,I want to do the following thing in c programming:
> a is now a sparse matrix of 100000 by 100000(for example)
> elem is the index matrix of 10000 by 3;
> for(i=0;i<10000;i++)
> {
> for(j=0;j<3;j++)
> for(k=0;k<3;k++)
> {
> jdex=elem(i,j);
> kdex=elem(i,k);
> a(jdex,kdex)=a(jdex,kdex)+f( ) /*f is some function to compute the value needed to be added*/
> }
> }
> Do you know any use friendly code about sparse matrix in mex file that contain the interface function as above?Thank you very much

The problem is this is EXTREMELY inefficient. Did
I emphasize that word enough?

Sparse matrix storage is efficient and fast to use,
but it requires your data to be essentially sorted in
memory. When you add a random single element
to that list, on average 1/2 of the elements in the
matrix must now be moved in memory.

This is why you do not want to do that operation,
not in matlab or in any language. Instead do NOT
build your matrix one element at a time. Keep a
list of all the elements in fully unsorted order, then
at the very end, build the entire matrix using sparse
(or whatever tool you wish to use) when you are
all done.

John

Subject: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?

From: Tim Davis

Date: 6 Jan, 2009 21:44:02

Message: 3 of 3

"zedong
" <zdongwu@gmail.com> wrote in message <gjph5h$a0o$1@fred.mathworks.com>...
> Is there any mex implementation of sparse matrix that I can add only one element to the matrix?
> I am working on finite element method.I want to use sparse matrix.But mex file has no function for sparse matrix for add only one element.
> for example,I want to do the following thing in c programming:
> a is now a sparse matrix of 100000 by 100000(for example)
> elem is the index matrix of 10000 by 3;
> for(i=0;i<10000;i++)
> {
> for(j=0;j<3;j++)
> for(k=0;k<3;k++)
> {
> jdex=elem(i,j);
> kdex=elem(i,k);
> a(jdex,kdex)=a(jdex,kdex)+f( ) /*f is some function to compute the value needed to be added*/
> }
> }
> Do you know any use friendly code about sparse matrix in mex file that contain the interface function as above?Thank you very much

Download SuiteSparse, and take a look at the functions in CSparse (for creating a triplet matrix and converting it to compressed-sparse-column). Or see the same functions in CHOLMOD, which do the same thing as "sparse" (just a different algorithm; often faster than the built-in "sparse"). Those functions do what John mentioned ... they build the matrix as a list of triplets and then convert them all at once to a compress-sparse-column matrix.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com