<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Creating Sparse Finite-Element Matrices in MATLAB</title>
	<atom:link href="http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Terence</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32297</link>
		<dc:creator>Terence</dc:creator>
		<pubDate>Tue, 31 May 2011 12:32:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32297</guid>
		<description>Dear Pat

Thanks for the suggestion.  I tried it and it works quite well, speeding up the algorithm by almost a factor of 10.

Out of interest I wrote a simple C code and used the mex function to put it in MATLAB.  While this was about 2 times faster than sparse2 for my application, your suggest works much better and avoids the need for all the mex function complexity (observation: just don&#039;t time the new code with the profiler; the overhead increases enormously as it goes through each step in the k loop!!)

Thanks
Terence</description>
		<content:encoded><![CDATA[<p>Dear Pat</p>
<p>Thanks for the suggestion.  I tried it and it works quite well, speeding up the algorithm by almost a factor of 10.</p>
<p>Out of interest I wrote a simple C code and used the mex function to put it in MATLAB.  While this was about 2 times faster than sparse2 for my application, your suggest works much better and avoids the need for all the mex function complexity (observation: just don&#8217;t time the new code with the profiler; the overhead increases enormously as it goes through each step in the k loop!!)</p>
<p>Thanks<br />
Terence</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pat</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32293</link>
		<dc:creator>Pat</dc:creator>
		<pubDate>Fri, 27 May 2011 18:10:44 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32293</guid>
		<description>Hi Terence.

If you only wish to use A, then consider the following:
&lt;pre&gt;
A = zeros(NG,1);
for k = 1:N
    A(col1(k)) = A(col1(k)) + col2(k);
end
&lt;/pre&gt;

While this is not quite as elegant as
&lt;pre&gt;
A = sparse(col1, 1, col2, NG, 1);
&lt;/pre&gt;
it is linear in the number of elements in col1 and avoids a sort.  On the other hand, if many entries of A wind up zero, then the code above will most likely use more memory.

Take care.
Pat.</description>
		<content:encoded><![CDATA[<p>Hi Terence.</p>
<p>If you only wish to use A, then consider the following:</p>
<pre>
A = zeros(NG,1);
for k = 1:N
    A(col1(k)) = A(col1(k)) + col2(k);
end
</pre>
<p>While this is not quite as elegant as</p>
<pre>
A = sparse(col1, 1, col2, NG, 1);
</pre>
<p>it is linear in the number of elements in col1 and avoids a sort.  On the other hand, if many entries of A wind up zero, then the code above will most likely use more memory.</p>
<p>Take care.<br />
Pat.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Terence</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32291</link>
		<dc:creator>Terence</dc:creator>
		<pubDate>Wed, 25 May 2011 03:26:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32291</guid>
		<description>Dear Loren

I recently encountered a problem that seems relevant to the current forum.

I have a column vector of positive integers (basically indices), and a second column vector of associated real numbers.  The task is basically to sort the first column in ascending order, while maintaing the correspondence with the second column.  Any repeated indices must result in the corresponding entries being summed.  Moving from for loops and if statements to the sparse and sparse2 functions resulted in very large savings in time.  However since the above operations must be performed some 10^5 or 10^6 times, I wonder if there is a faster method than using sparse?  Would C++ or fortran be able to do such a task faster?

A modified example of the code is as follows (for other reasons the for loop cannot be removed, Nt here is only 10, but needs to be 10^5 or 10^6):

&lt;pre&gt;
NG = 20000;
N = 5e5;
Nt = 10;

for loop = 1 : Nt 
    
   col1 = floor(NG*rand(N, 1)) + 1; 
   col2 = rand(N, 1);
   
   A = sparse(col1, 1, col2, NG, 1);
    
end
&lt;/pre&gt; 

The A vector is then used elsewhere (not shown).

Thanks
Terence</description>
		<content:encoded><![CDATA[<p>Dear Loren</p>
<p>I recently encountered a problem that seems relevant to the current forum.</p>
<p>I have a column vector of positive integers (basically indices), and a second column vector of associated real numbers.  The task is basically to sort the first column in ascending order, while maintaing the correspondence with the second column.  Any repeated indices must result in the corresponding entries being summed.  Moving from for loops and if statements to the sparse and sparse2 functions resulted in very large savings in time.  However since the above operations must be performed some 10^5 or 10^6 times, I wonder if there is a faster method than using sparse?  Would C++ or fortran be able to do such a task faster?</p>
<p>A modified example of the code is as follows (for other reasons the for loop cannot be removed, Nt here is only 10, but needs to be 10^5 or 10^6):</p>
<pre>
NG = 20000;
N = 5e5;
Nt = 10;

for loop = 1 : Nt 

   col1 = floor(NG*rand(N, 1)) + 1;
   col2 = rand(N, 1);

   A = sparse(col1, 1, col2, NG, 1);

end
</pre>
<p>The A vector is then used elsewhere (not shown).</p>
<p>Thanks<br />
Terence</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32282</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 23 May 2011 18:56:09 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32282</guid>
		<description>Meenakshi,

If the locations are the same, which I still can&#039;t tell from your response, then you should be okay.  If the sparsity is the same, you will have some overhead, but not more than creating the sparse matrix from scratch, I think.  You can try it out and time it yourself.

--Loren</description>
		<content:encoded><![CDATA[<p>Meenakshi,</p>
<p>If the locations are the same, which I still can&#8217;t tell from your response, then you should be okay.  If the sparsity is the same, you will have some overhead, but not more than creating the sparse matrix from scratch, I think.  You can try it out and time it yourself.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Meenakshi Sundaram</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32281</link>
		<dc:creator>Meenakshi Sundaram</dc:creator>
		<pubDate>Mon, 23 May 2011 18:52:42 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32281</guid>
		<description>Dear Loren,

The sparsity pattern remains the same. But the entries in the filled location of the matrix have to be cleared out and then re-filled in with fresh values every iteration. 

Meenakshi</description>
		<content:encoded><![CDATA[<p>Dear Loren,</p>
<p>The sparsity pattern remains the same. But the entries in the filled location of the matrix have to be cleared out and then re-filled in with fresh values every iteration. </p>
<p>Meenakshi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32280</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 23 May 2011 18:37:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32280</guid>
		<description>Meenakshi-

Not if the zeros stay in the same place each time, which is what you wrote above.

--Loren</description>
		<content:encoded><![CDATA[<p>Meenakshi-</p>
<p>Not if the zeros stay in the same place each time, which is what you wrote above.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Meenakshi Sundaram</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32279</link>
		<dc:creator>Meenakshi Sundaram</dc:creator>
		<pubDate>Mon, 23 May 2011 18:20:58 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32279</guid>
		<description>Loren,

But it needs zeroing it out the entries every time which I think causes it to loose the allocated memory. Any other clever suggestions would be really helpful. 

Meenakshi</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>But it needs zeroing it out the entries every time which I think causes it to loose the allocated memory. Any other clever suggestions would be really helpful. </p>
<p>Meenakshi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32276</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 23 May 2011 10:46:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32276</guid>
		<description>Meenaskshi-

That would be the best idea for not thrashing memory.

--Loren</description>
		<content:encoded><![CDATA[<p>Meenaskshi-</p>
<p>That would be the best idea for not thrashing memory.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Meenakshi Sundaram</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32275</link>
		<dc:creator>Meenakshi Sundaram</dc:creator>
		<pubDate>Mon, 23 May 2011 09:49:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32275</guid>
		<description>Hi Loren,

I am developing a code for non-linear FEM in matlab. I notice that the same sparsity pattern occurs during the assembly of the matrix in every iteration.

Should I create the sparse matrix only once in the first iteration and dereference it again and again by using 
&lt;pre&gt;
K(element_dofs,element_dofs)=K(element_dofs,element_dofs)+ elem_K;
&lt;/pre&gt;
as opposed to assembling it again and again in every iteration.

It boils down to whether dereferencing a created sparse matrix is faster in comparison to creating one?

-Meenakshi</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>I am developing a code for non-linear FEM in matlab. I notice that the same sparsity pattern occurs during the assembly of the matrix in every iteration.</p>
<p>Should I create the sparse matrix only once in the first iteration and dereference it again and again by using </p>
<pre>
K(element_dofs,element_dofs)=K(element_dofs,element_dofs)+ elem_K;
</pre>
<p>as opposed to assembling it again and again in every iteration.</p>
<p>It boils down to whether dereferencing a created sparse matrix is faster in comparison to creating one?</p>
<p>-Meenakshi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/#comment-32149</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Sun, 27 Mar 2011 09:16:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=79#comment-32149</guid>
		<description>Wahyoe-

Please contact technical support (link on right of blog page) with full information of your computer configuration so they can help you decipher wha tis happening.

I suspect your progam is asking for more memory than it can find.

--Loren</description>
		<content:encoded><![CDATA[<p>Wahyoe-</p>
<p>Please contact technical support (link on right of blog page) with full information of your computer configuration so they can help you decipher wha tis happening.</p>
<p>I suspect your progam is asking for more memory than it can find.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
</channel>
</rss>

