<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113</link>
    <title>MATLAB Central Newsreader - Return empty sparse from a mex file</title>
    <description>Feed for thread: Return empty sparse from a mex file</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>Mon, 26 Oct 2009 10:02:02 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#689717</link>
      <author>Arne Vagren</author>
      <description>Hello!&lt;br&gt;
&lt;br&gt;
I'm trying to assemble and return a sparse matrix in a mex-function. At the end of the function, I try to correct for the actual number of non-zero entries in the following way (where Btu = mxGetPr(plhs[0]);):&lt;br&gt;
&lt;br&gt;
&quot;nbytes = actual_number_of_nonzeros*sizeof(*Btu);&lt;br&gt;
&amp;nbsp;newptr = mxRealloc(Btu,nbytes);     &lt;br&gt;
&amp;nbsp;mxSetPr(plhs[0], newptr); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;nbytes = actual_number_of_nonzeros*sizeof(*ir);&lt;br&gt;
&amp;nbsp;newptr = mxRealloc(ir, nbytes);&lt;br&gt;
&amp;nbsp;mxSetIr(plhs[0], newptr);&quot;&lt;br&gt;
&lt;br&gt;
To test my function I have tried , the, excellent, spok-function, see &lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok,&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok,&lt;/a&gt; which gives an ok except when the output is empty, i.e. nnz = 0. spok then returns the error message &quot;double, but with no values present&quot;. The resulting matrix still seem to work fine but it's a little disturbing to have an error which I don't understand.&lt;br&gt;
&lt;br&gt;
My question is thus: What should my function return if actual_number_of_nonzeros = 0?&lt;br&gt;
&lt;br&gt;
Best regards&lt;br&gt;
Arne</description>
    </item>
    <item>
      <pubDate>Mon, 26 Oct 2009 10:49:01 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#689721</link>
      <author>Bruno Luong</author>
      <description>&quot;Arne Vagren&quot; &amp;lt;arne.vagren@gmail.com&amp;gt; wrote in message &amp;lt;hc3s2q$fbd$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm trying to assemble and return a sparse matrix in a mex-function. At the end of the function, I try to correct for the actual number of non-zero entries in the following way (where Btu = mxGetPr(plhs[0]);):&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;nbytes = actual_number_of_nonzeros*sizeof(*Btu);&lt;br&gt;
&amp;gt;  newptr = mxRealloc(Btu,nbytes);     &lt;br&gt;
&amp;gt;  mxSetPr(plhs[0], newptr); &lt;br&gt;
&amp;gt;         &lt;br&gt;
&amp;gt;  nbytes = actual_number_of_nonzeros*sizeof(*ir);&lt;br&gt;
&amp;gt;  newptr = mxRealloc(ir, nbytes);&lt;br&gt;
&amp;gt;  mxSetIr(plhs[0], newptr);&quot;&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
There is a possibly problem in Jc array, which you didn't provide information.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 26 Oct 2009 11:19:02 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#689731</link>
      <author>Arne Vagren</author>
      <description>Thanks for your reply. Actually, in the case nnz = 0, I haven't touched the jc array, it's&lt;br&gt;
simply an array of zeros of size [number of columns + 1]</description>
    </item>
    <item>
      <pubDate>Mon, 26 Oct 2009 11:45:18 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#689736</link>
      <author>Bruno Luong</author>
      <description>&quot;Arne Vagren&quot; &amp;lt;arne.vagren@gmail.com&amp;gt; wrote in message &amp;lt;hc40j6$o3d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thanks for your reply. Actually, in the case nnz = 0, I haven't touched the jc array, it's&lt;br&gt;
&amp;gt; simply an array of zeros of size [number of columns + 1]&lt;br&gt;
&lt;br&gt;
Have you changed nzmax to zero accordingly (by using mxSetNzmax)?&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 26 Oct 2009 11:50:18 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#689738</link>
      <author>Bruno Luong</author>
      <description>Actually I just check, it seems nzmax is never set to 0 by Matlab&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; s=sparse([],[],[],1,1,0)&lt;br&gt;
&amp;gt;&amp;gt; nzmax(s)&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;br&gt;
&lt;br&gt;
So I would Realloc Pr and Ir with *1* element, set nzmax to 1, and fill Jc with zeros (as you did).&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 26 Oct 2009 12:18:02 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#689743</link>
      <author>Arne Vagren</author>
      <description>Ok, I added the line&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (actual_number_of_nonzeros==0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;actual_number_of_nonzeros = 1;&lt;br&gt;
&lt;br&gt;
before reallocating Pr and Ir. It works fine now, although it seem a little odd to me. Thanks again for your comments!&lt;br&gt;
/Arne&lt;br&gt;
&lt;br&gt;
&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;hc42dq$ke8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Actually I just check, it seems nzmax is never set to 0 by Matlab&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; s=sparse([],[],[],1,1,0)&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; nzmax(s)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So I would Realloc Pr and Ir with *1* element, set nzmax to 1, and fill Jc with zeros (as you did).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 30 Oct 2009 16:11:03 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#690997</link>
      <author>Tim Davis</author>
      <description>&quot;Arne Vagren&quot; &amp;lt;arne.vagren@gmail.com&amp;gt; wrote in message &amp;lt;hc3s2q$fbd$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm trying to assemble and return a sparse matrix in a mex-function. At the end of the function, I try to correct for the actual number of non-zero entries in the following way (where Btu = mxGetPr(plhs[0]);):&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;nbytes = actual_number_of_nonzeros*sizeof(*Btu);&lt;br&gt;
&amp;gt;  newptr = mxRealloc(Btu,nbytes);     &lt;br&gt;
&amp;gt;  mxSetPr(plhs[0], newptr); &lt;br&gt;
&amp;gt;         &lt;br&gt;
&amp;gt;  nbytes = actual_number_of_nonzeros*sizeof(*ir);&lt;br&gt;
&amp;gt;  newptr = mxRealloc(ir, nbytes);&lt;br&gt;
&amp;gt;  mxSetIr(plhs[0], newptr);&quot;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To test my function I have tried , the, excellent, spok-function, see &lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok,&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok,&lt;/a&gt; which gives an ok except when the output is empty, i.e. nnz = 0. spok then returns the error message &quot;double, but with no values present&quot;. The resulting matrix still seem to work fine but it's a little disturbing to have an error which I don't understand.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My question is thus: What should my function return if actual_number_of_nonzeros = 0?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Best regards&lt;br&gt;
&amp;gt; Arne&lt;br&gt;
&lt;br&gt;
You are right to be troubled; spok is correctly telling you that you have constructed an invalid matrix.  That matrix, with a NULL pointer for mxGetPr, will break MATLAB somewhere.  Not everywhere, so it may seem fine, but it is not valid.&lt;br&gt;
&lt;br&gt;
nnzmax(A) must never be zero, even if nnz(A) is zero.  nnzmax(A)&amp;gt;=1 must always hold.</description>
    </item>
    <item>
      <pubDate>Fri, 30 Oct 2009 17:46:03 -0400</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#691028</link>
      <author>James Tursa</author>
      <description>&quot;Tim Davis&quot; &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message &amp;lt;hcf36n$83n$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You are right to be troubled; spok is correctly telling you that you have constructed an invalid matrix.  That matrix, with a NULL pointer for mxGetPr, will break MATLAB somewhere.  Not everywhere, so it may seem fine, but it is not valid.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; nnzmax(A) must never be zero, even if nnz(A) is zero.  nnzmax(A)&amp;gt;=1 must always hold.&lt;br&gt;
&lt;br&gt;
FYI added info just to complete this thread. There is a difference between full matrices and sparse matrices on this point. If you examine the pr of a full empty matrix, it can be NULL (0), whereas for a sparse matrix it always points to a valid memory location.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 19:19:20 -0500</pubDate>
      <title>Return empty sparse from a mex file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264113#691810</link>
      <author>Arne Vagren</author>
      <description>Thanks! I guess that's a rather subtle error that could have caused me quite a bit of frustration...</description>
    </item>
  </channel>
</rss>

