<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241868</link>
    <title>MATLAB Central Newsreader - Is there any mex implementation of sparse matrix that I can add only one element to the matrix?</title>
    <description>Feed for thread: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 04 Jan 2009 05:24:01 -0500</pubDate>
      <title>Is there any mex implementation of sparse matrix that I can add only one element to the matrix?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241868#619783</link>
      <author>zedong </author>
      <description>Is there any mex implementation of sparse matrix that I can add only one element to the matrix?&lt;br&gt;
&amp;nbsp;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.&lt;br&gt;
for example,I want to do the following thing in c programming:&lt;br&gt;
a is now a sparse matrix of 100000 by 100000(for example)&lt;br&gt;
elem is the index matrix of 10000 by 3;&lt;br&gt;
for(i=0;i&amp;lt;10000;i++)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(j=0;j&amp;lt;3;j++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(k=0;k&amp;lt;3;k++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;jdex=elem(i,j);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;kdex=elem(i,k);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a(jdex,kdex)=a(jdex,kdex)+f( )  /*f is some function to compute the value needed to be added*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;br&gt;
Do you know any use friendly code about sparse matrix in mex file that contain the interface  function as above?Thank you very much</description>
    </item>
    <item>
      <pubDate>Sun, 04 Jan 2009 09:56:02 -0500</pubDate>
      <title>Re: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241868#619790</link>
      <author>John D'Errico</author>
      <description>&quot;zedong &lt;br&gt;
&quot; &amp;lt;zdongwu@gmail.com&amp;gt; wrote in message &amp;lt;gjph5h$a0o$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Is there any mex implementation of sparse matrix that I can add only one element to the matrix?&lt;br&gt;
&amp;gt;  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.&lt;br&gt;
&amp;gt; for example,I want to do the following thing in c programming:&lt;br&gt;
&amp;gt; a is now a sparse matrix of 100000 by 100000(for example)&lt;br&gt;
&amp;gt; elem is the index matrix of 10000 by 3;&lt;br&gt;
&amp;gt; for(i=0;i&amp;lt;10000;i++)&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;         for(j=0;j&amp;lt;3;j++)&lt;br&gt;
&amp;gt;         for(k=0;k&amp;lt;3;k++)&lt;br&gt;
&amp;gt;        {&lt;br&gt;
&amp;gt;               jdex=elem(i,j);&lt;br&gt;
&amp;gt;               kdex=elem(i,k);&lt;br&gt;
&amp;gt;                a(jdex,kdex)=a(jdex,kdex)+f( )  /*f is some function to compute the value needed to be added*/&lt;br&gt;
&amp;gt;        }&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; Do you know any use friendly code about sparse matrix in mex file that contain the interface  function as above?Thank you very much&lt;br&gt;
&lt;br&gt;
The problem is this is EXTREMELY inefficient. Did&lt;br&gt;
I emphasize that word enough? &lt;br&gt;
&lt;br&gt;
Sparse matrix storage is efficient and fast to use,&lt;br&gt;
but it requires your data to be essentially sorted in&lt;br&gt;
memory. When you add a random single element&lt;br&gt;
to that list, on average 1/2 of the elements in the&lt;br&gt;
matrix must now be moved in memory.&lt;br&gt;
&lt;br&gt;
This is why you do not want to do that operation,&lt;br&gt;
not in matlab or in any language. Instead do NOT&lt;br&gt;
build your matrix one element at a time. Keep a&lt;br&gt;
list of all the elements in fully unsorted order, then&lt;br&gt;
at the very end, build the entire matrix using sparse&lt;br&gt;
(or whatever tool you wish to use) when you are&lt;br&gt;
all done.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Tue, 06 Jan 2009 21:44:02 -0500</pubDate>
      <title>Re: Is there any mex implementation of sparse matrix that I can add only one element to the matrix?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241868#620176</link>
      <author>Tim Davis</author>
      <description>&quot;zedong &lt;br&gt;
&quot; &amp;lt;zdongwu@gmail.com&amp;gt; wrote in message &amp;lt;gjph5h$a0o$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Is there any mex implementation of sparse matrix that I can add only one element to the matrix?&lt;br&gt;
&amp;gt;  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.&lt;br&gt;
&amp;gt; for example,I want to do the following thing in c programming:&lt;br&gt;
&amp;gt; a is now a sparse matrix of 100000 by 100000(for example)&lt;br&gt;
&amp;gt; elem is the index matrix of 10000 by 3;&lt;br&gt;
&amp;gt; for(i=0;i&amp;lt;10000;i++)&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;         for(j=0;j&amp;lt;3;j++)&lt;br&gt;
&amp;gt;         for(k=0;k&amp;lt;3;k++)&lt;br&gt;
&amp;gt;        {&lt;br&gt;
&amp;gt;               jdex=elem(i,j);&lt;br&gt;
&amp;gt;               kdex=elem(i,k);&lt;br&gt;
&amp;gt;                a(jdex,kdex)=a(jdex,kdex)+f( )  /*f is some function to compute the value needed to be added*/&lt;br&gt;
&amp;gt;        }&lt;br&gt;
&amp;gt; }&lt;br&gt;
&amp;gt; Do you know any use friendly code about sparse matrix in mex file that contain the interface  function as above?Thank you very much&lt;br&gt;
&lt;br&gt;
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 &quot;sparse&quot; (just a different algorithm; often faster than the built-in &quot;sparse&quot;).  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.</description>
    </item>
  </channel>
</rss>

