Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?
Date: Sun, 4 Jan 2009 09:56:02 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 37
Message-ID: <gjq13i$25q$1@fred.mathworks.com>
References: <gjph5h$a0o$1@fred.mathworks.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1231062962 2234 172.30.248.37 (4 Jan 2009 09:56:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 4 Jan 2009 09:56:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:509736


"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