<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186</link>
    <title>MATLAB Central Newsreader - Convert array of class 'int8' to sparse</title>
    <description>Feed for thread: Convert array of class 'int8' to sparse</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>Tue, 24 Mar 2009 01:03:02 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637121</link>
      <author>Gus Lott</author>
      <description>I too am looking for this feature to come to matlab's sparse matrix tools.</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 09:25:05 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637176</link>
      <author>James Tursa</author>
      <description>&quot;Gus Lott&quot; &amp;lt;lottg.nospam@janelia.hhmi.org&amp;gt; wrote in message &amp;lt;gq9bg5$3ra$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I too am looking for this feature to come to matlab's sparse matrix tools.&lt;br&gt;
&lt;br&gt;
All,&lt;br&gt;
&lt;br&gt;
You could always just modify the MATLAB mex example fulltosparse.c of filling a sparse matrix and change the input from double to int8. The Mathworks comments seem to imply that this is the very purpose of the example code. E.g., the code is below. To mex it just do this:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; mex -setup&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(then select a C/C++ compiler, such as lcc)&lt;br&gt;
&amp;gt;&amp;gt; mex int8tosparse.c&lt;br&gt;
&lt;br&gt;
Now you have a function int8tosparse that takes a 2-dimensional int8 input and directly converts it into a sparse double matrix without taking the intermediate step of converting to a full double matrix first. If there is some interest in this, I could work this up into a complete mex function that works for *all* integer classes and post it to the FEX. Let me know.&lt;br&gt;
&lt;br&gt;
James Tursa&lt;br&gt;
&lt;br&gt;
Caveat, I only tested this for a single 100x100 example. It got the same answer as converting to a double matrix first, so I would assume the code is correct. However, be advised this is the Mathworks example code, not mine. I just modified it for int8. If you have a later version of MATLAB you might want to change the int j,k,etc to mwSize j,k,etc.&lt;br&gt;
&lt;br&gt;
-----------------------------------------------------------------------------------------------------&lt;br&gt;
&lt;br&gt;
/*&lt;br&gt;
&amp;nbsp;* =============================================================&lt;br&gt;
&amp;nbsp;* int8tosparse.c&lt;br&gt;
&amp;nbsp;* This example demonstrates how to populate a sparse&lt;br&gt;
&amp;nbsp;* matrix.  For the purpose of this example, you must pass in a&lt;br&gt;
&amp;nbsp;* non-sparse 2-dimensional argument of type int8.&lt;br&gt;
&amp;nbsp;*&lt;br&gt;
&amp;nbsp;* Mathworks fulltosparse.c modified by J. Tursa for int8.&lt;br&gt;
&amp;nbsp;*&lt;br&gt;
=============================================================&lt;br&gt;
&amp;nbsp;*/&lt;br&gt;
&lt;br&gt;
#include &amp;lt;math.h&amp;gt; /* Needed for the ceil() prototype. */&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
&lt;br&gt;
void mexFunction(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int nlhs,       mxArray *plhs[],&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int nrhs, const mxArray *prhs[]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Declare variables. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;int j,k,m,n,nzmax,*irs,*jcs,cmplx,isfull;&lt;br&gt;
&amp;nbsp;&amp;nbsp;double *si,*sr;&lt;br&gt;
&amp;nbsp;&amp;nbsp;double percent_sparse;&lt;br&gt;
&amp;nbsp;&amp;nbsp;signed char *pr, *pi;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Check for proper number of input and output arguments. */    &lt;br&gt;
&amp;nbsp;&amp;nbsp;if (nrhs != 1) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;One input argument required.&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;} &lt;br&gt;
&amp;nbsp;&amp;nbsp;if (nlhs &amp;gt; 1) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;Too many output arguments.&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Check data type of input argument. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (!(mxIsInt8(prhs[0]))) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;Input argument must be of type int8.&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;} &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (mxGetNumberOfDimensions(prhs[0]) != 2) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;Input argument must be two dimensional\n&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Get the size and pointers to input data. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;m  = mxGetM(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;n  = mxGetN(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;pr = mxGetPr(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;pi = mxGetPi(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;cmplx = (pi == NULL ? 0 : 1);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Allocate space for sparse matrix. &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;* NOTE:  Assume at most 20% of the data is sparse.  Use ceil&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;* to cause it to round up. &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;percent_sparse = 0.2;&lt;br&gt;
&amp;nbsp;&amp;nbsp;nzmax = (int)ceil((double)m*(double)n*percent_sparse);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;plhs[0] = mxCreateSparse(m,n,nzmax,cmplx);&lt;br&gt;
&amp;nbsp;&amp;nbsp;sr  = mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;si  = mxGetPi(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;irs = mxGetIr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;jcs = mxGetJc(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* Copy nonzeros. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;k = 0; &lt;br&gt;
&amp;nbsp;&amp;nbsp;isfull = 0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;for (j = 0; (j &amp;lt; n); j++) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;jcs[j] = k;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (i = 0; (i &amp;lt; m); i++) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (pr[i] || (cmplx &amp;&amp; pi[i])) {&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Check to see if non-zero element will fit in  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* allocated output array.  If not, increase &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* percent_sparse by 10%, recalculate nzmax, and augment &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;* the sparse array. &lt;br&gt;
&amp;nbsp;&amp;nbsp;&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;if (k &amp;gt;= nzmax) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int oldnzmax = nzmax;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;percent_sparse += 0.1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nzmax = (int)ceil((double)m*(double)n*percent_sparse);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Make sure nzmax increases atleast by 1. */&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (oldnzmax == nzmax) &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;nzmax++;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxSetNzmax(plhs[0], nzmax); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxSetPr(plhs[0], mxRealloc(sr, nzmax*sizeof(double)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (si != NULL)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxSetPi(plhs[0], mxRealloc(si, nzmax*sizeof(double)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mxSetIr(plhs[0], mxRealloc(irs, nzmax*sizeof(int)));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sr  = mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;si  = mxGetPi(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;irs = mxGetIr(plhs[0]);&lt;br&gt;
&amp;nbsp;&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;sr[k] = pr[i];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (cmplx) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;si[k] = pi[i];&lt;br&gt;
&amp;nbsp;&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;irs[k] = i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;k++;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pr += m;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pi += m;&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;jcs[n] = k;&lt;br&gt;
} </description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 10:55:03 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637190</link>
      <author>Derek O'Connor</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in message &amp;lt;gqa8th$7j0$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Gus Lott&quot; &amp;lt;lottg.nospam@janelia.hhmi.org&amp;gt; wrote in message &amp;lt;gq9bg5$3ra$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I too am looking for this feature to come to matlab's sparse matrix tools.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; All,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You could always just modify the MATLAB mex example fulltosparse.c of filling a sparse matrix and change the input from double to int8. The Mathworks comments seem to imply that this is the very purpose of the example code. E.g., the code is below. To mex it just do this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; mex -setup&lt;br&gt;
&amp;gt;     (then select a C/C++ compiler, such as lcc)&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; mex int8tosparse.c&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now you have a function int8tosparse that takes a 2-dimensional int8 input and directly converts it into a sparse double matrix without taking the intermediate step of converting to a full double matrix first. If there is some interest in this, I could work this up into a complete mex function that works for *all* integer classes and post it to the FEX. Let me know.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Caveat, I only tested this for a single 100x100 example. It got the same answer as converting to a double matrix first, so I would assume the code is correct. However, be advised this is the Mathworks example code, not mine. I just modified it for int8. If you have a later version of MATLAB you might want to change the int j,k,etc to mwSize j,k,etc.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -----------------------------------------------------------------------------------------------------&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; /*&lt;br&gt;
&amp;gt;  * =============================================================&lt;br&gt;
&amp;gt;  * int8tosparse.c&lt;br&gt;
&amp;gt;  * This example demonstrates how to populate a sparse&lt;br&gt;
&amp;gt;  * matrix.  For the purpose of this example, you must pass in a&lt;br&gt;
&amp;gt;  * non-sparse 2-dimensional argument of type int8.&lt;br&gt;
&amp;gt;  *&lt;br&gt;
&amp;gt;  * Mathworks fulltosparse.c modified by J. Tursa for int8.&lt;br&gt;
&amp;gt;  *&lt;br&gt;
&amp;gt; =============================================================&lt;br&gt;
&amp;gt;  */&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; #include &amp;lt;math.h&amp;gt; /* Needed for the ceil() prototype. */&lt;br&gt;
&amp;gt; #include &quot;mex.h&quot;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; void mexFunction(&lt;br&gt;
&amp;gt;         int nlhs,       mxArray *plhs[],&lt;br&gt;
&amp;gt;         int nrhs, const mxArray *prhs[]&lt;br&gt;
&amp;gt;         )&lt;br&gt;
&amp;gt; {&lt;br&gt;
&amp;gt;   /* Declare variables. */&lt;br&gt;
&amp;gt;   int j,k,m,n,nzmax,*irs,*jcs,cmplx,isfull;&lt;br&gt;
&amp;gt;   double *si,*sr;&lt;br&gt;
&amp;gt;   double percent_sparse;&lt;br&gt;
&amp;gt;   signed char *pr, *pi;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   /* Check for proper number of input and output arguments. */    &lt;br&gt;
&amp;gt;   if (nrhs != 1) {&lt;br&gt;
&amp;gt;     mexErrMsgTxt(&quot;One input argument required.&quot;);&lt;br&gt;
&amp;gt;   } &lt;br&gt;
&amp;gt;   if (nlhs &amp;gt; 1) {&lt;br&gt;
&amp;gt;     mexErrMsgTxt(&quot;Too many output arguments.&quot;);&lt;br&gt;
&amp;gt;   }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   /* Check data type of input argument. */&lt;br&gt;
&amp;gt;   if (!(mxIsInt8(prhs[0]))) {&lt;br&gt;
&amp;gt;     mexErrMsgTxt(&quot;Input argument must be of type int8.&quot;);&lt;br&gt;
&amp;gt;   } &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   if (mxGetNumberOfDimensions(prhs[0]) != 2) {&lt;br&gt;
&amp;gt;     mexErrMsgTxt(&quot;Input argument must be two dimensional\n&quot;);&lt;br&gt;
&amp;gt;   }&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   /* Get the size and pointers to input data. */&lt;br&gt;
&amp;gt;   m  = mxGetM(prhs[0]);&lt;br&gt;
&amp;gt;   n  = mxGetN(prhs[0]);&lt;br&gt;
&amp;gt;   pr = mxGetPr(prhs[0]);&lt;br&gt;
&amp;gt;   pi = mxGetPi(prhs[0]);&lt;br&gt;
&amp;gt;   cmplx = (pi == NULL ? 0 : 1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   /* Allocate space for sparse matrix. &lt;br&gt;
&amp;gt;    * NOTE:  Assume at most 20% of the data is sparse.  Use ceil&lt;br&gt;
&amp;gt;    * to cause it to round up. &lt;br&gt;
&amp;gt;    */&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   percent_sparse = 0.2;&lt;br&gt;
&amp;gt;   nzmax = (int)ceil((double)m*(double)n*percent_sparse);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   plhs[0] = mxCreateSparse(m,n,nzmax,cmplx);&lt;br&gt;
&amp;gt;   sr  = mxGetPr(plhs[0]);&lt;br&gt;
&amp;gt;   si  = mxGetPi(plhs[0]);&lt;br&gt;
&amp;gt;   irs = mxGetIr(plhs[0]);&lt;br&gt;
&amp;gt;   jcs = mxGetJc(plhs[0]);&lt;br&gt;
&amp;gt;     &lt;br&gt;
&amp;gt;   /* Copy nonzeros. */&lt;br&gt;
&amp;gt;   k = 0; &lt;br&gt;
&amp;gt;   isfull = 0;&lt;br&gt;
&amp;gt;   for (j = 0; (j &amp;lt; n); j++) {&lt;br&gt;
&amp;gt;     int i;&lt;br&gt;
&amp;gt;     jcs[j] = k;&lt;br&gt;
&amp;gt;     for (i = 0; (i &amp;lt; m); i++) {&lt;br&gt;
&amp;gt;       if (pr[i] || (cmplx &amp;&amp; pi[i])) {&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;         /* Check to see if non-zero element will fit in  &lt;br&gt;
&amp;gt;          * allocated output array.  If not, increase &lt;br&gt;
&amp;gt;          * percent_sparse by 10%, recalculate nzmax, and augment &lt;br&gt;
&amp;gt;          * the sparse array. &lt;br&gt;
&amp;gt;          */&lt;br&gt;
&amp;gt;         if (k &amp;gt;= nzmax) {&lt;br&gt;
&amp;gt;           int oldnzmax = nzmax;&lt;br&gt;
&amp;gt;           percent_sparse += 0.1;&lt;br&gt;
&amp;gt;           nzmax = (int)ceil((double)m*(double)n*percent_sparse);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;           /* Make sure nzmax increases atleast by 1. */&lt;br&gt;
&amp;gt;           if (oldnzmax == nzmax) &lt;br&gt;
&amp;gt;             nzmax++;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;           mxSetNzmax(plhs[0], nzmax); &lt;br&gt;
&amp;gt;           mxSetPr(plhs[0], mxRealloc(sr, nzmax*sizeof(double)));&lt;br&gt;
&amp;gt;           if (si != NULL)&lt;br&gt;
&amp;gt;           mxSetPi(plhs[0], mxRealloc(si, nzmax*sizeof(double)));&lt;br&gt;
&amp;gt;           mxSetIr(plhs[0], mxRealloc(irs, nzmax*sizeof(int)));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;           sr  = mxGetPr(plhs[0]);&lt;br&gt;
&amp;gt;           si  = mxGetPi(plhs[0]);&lt;br&gt;
&amp;gt;           irs = mxGetIr(plhs[0]);&lt;br&gt;
&amp;gt;         }&lt;br&gt;
&amp;gt;         sr[k] = pr[i];&lt;br&gt;
&amp;gt;         if (cmplx) {&lt;br&gt;
&amp;gt;           si[k] = pi[i];&lt;br&gt;
&amp;gt;         }&lt;br&gt;
&amp;gt;         irs[k] = i;&lt;br&gt;
&amp;gt;         k++;&lt;br&gt;
&amp;gt;       }&lt;br&gt;
&amp;gt;     }&lt;br&gt;
&amp;gt;     pr += m;&lt;br&gt;
&amp;gt;     pi += m;&lt;br&gt;
&amp;gt;   }&lt;br&gt;
&amp;gt;   jcs[n] = k;&lt;br&gt;
&amp;gt; } &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Dear John,&lt;br&gt;
&lt;br&gt;
With&lt;br&gt;
----------------------------------------------------&lt;br&gt;
&amp;gt;&amp;gt; ver&lt;br&gt;
MATLAB Version 7.6.0.324 (R2008a)&lt;br&gt;
Operating System: Microsoft Windows Vista Version 6.0&lt;br&gt;
&amp;nbsp;(Build 6001: Service Pack 1)&lt;br&gt;
Java VM Version: Java 1.6.0 with Sun Microsystems Inc.&lt;br&gt;
Java HotSpot(TM) 64-Bit Server VM mixed mode&lt;br&gt;
-----------------------------------------------------&lt;br&gt;
&lt;br&gt;
I ran&lt;br&gt;
&lt;br&gt;
-----------------------------------------------------&lt;br&gt;
&amp;gt;&amp;gt; mex -largeArrayDims int8tosparse.c&lt;br&gt;
Microsoft (R) C/C++ Optimizing Compiler Version&lt;br&gt;
15.00.21022.08 for x64 Copyright (C) Microsoft&lt;br&gt;
Corporation.  All rights reserved.&lt;br&gt;
-----------------------------------------------------&lt;br&gt;
&lt;br&gt;
which generated the file int8tosparse.mexw64&lt;br&gt;
&lt;br&gt;
I then ran&lt;br&gt;
&lt;br&gt;
--------------------------------------------------------&lt;br&gt;
&amp;gt;&amp;gt; n = 1000; Sp16 = sprand(n,n,0.001); D16 = full(Sp16);...&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;D8 = int8(D16); Sp8 = int8tosparse(D8);...&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;whos&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Name         Size                       Bytes  Class       Attributes&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Sp16     1000x1000                24,008   double    sparse&lt;br&gt;
&amp;nbsp;&amp;nbsp;D16      1000x1000            8,000,000   double&lt;br&gt;
&amp;nbsp;&amp;nbsp;D8        1000x1000            1,000,000   int8&lt;br&gt;
&amp;nbsp;&amp;nbsp;Sp8       1000x1000           3,208,008   double    sparse&lt;br&gt;
&amp;nbsp;&amp;nbsp;n           1x1                     8  double&lt;br&gt;
&lt;br&gt;
commas added so I can read the sizes&lt;br&gt;
-----------------------------------------------------------&lt;br&gt;
&lt;br&gt;
I know nothing about C/C++ programming and compiling. Any suggestions?&lt;br&gt;
&lt;br&gt;
A complete mex function that works for *all* integer classes would be very useful, especially when working with large sparse graphs.&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
&lt;br&gt;
Derek O'Connor</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 14:23:01 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637255</link>
      <author>James Tursa</author>
      <description>&quot;Derek O'Connor&quot; &amp;lt;derekroconnor@eircom.net&amp;gt; wrote in message &amp;lt;gqae67$vg$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I then ran&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; --------------------------------------------------------&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; n = 1000; Sp16 = sprand(n,n,0.001); D16 = full(Sp16);...&lt;br&gt;
&amp;gt;                D8 = int8(D16); Sp8 = int8tosparse(D8);...&lt;br&gt;
&amp;gt;                whos&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Name         Size                       Bytes  Class       Attributes&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Sp16     1000x1000                24,008   double    sparse&lt;br&gt;
&amp;gt;   D16      1000x1000            8,000,000   double&lt;br&gt;
&amp;gt;   D8        1000x1000            1,000,000   int8&lt;br&gt;
&amp;gt;   Sp8       1000x1000           3,208,008   double    sparse&lt;br&gt;
&amp;gt;   n           1x1                     8  double&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; commas added so I can read the sizes&lt;br&gt;
&amp;gt; -----------------------------------------------------------&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I know nothing about C/C++ programming and compiling. Any suggestions?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A complete mex function that works for *all* integer classes would be very useful, especially when working with large sparse graphs.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Derek O'Connor&lt;br&gt;
&lt;br&gt;
First, you are comparing apples to oranges. You need to make sure the original sparse matrix can be converted to int8 and still get the same values before any comparison will be meaningful. i.e., make sure the original values are all integral between -128 and +127. Second, you cannot just compare storage size to see if the conversion happened correctly, you need to use the isequal function to determine this. Third, the code guesses that the storage needed is 20% and then incrementally bumps up the memory requirement by 10% if needed. This may be too large of a guess for your case, and would need to be made smarter for general cases. Finally, for your later version of MATLAB I would change int to mwSize as I mentioned earlier. So change the c code lines:&lt;br&gt;
&lt;br&gt;
int j,k,m,n,nzmax,*irs,*jcs,cmplx,isfull;&lt;br&gt;
&lt;br&gt;
to&lt;br&gt;
&lt;br&gt;
mwSize j,k,m,n,nzmax,*irs,*jcs,cmplx,isfull;&lt;br&gt;
&lt;br&gt;
and&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;percent_sparse = 0.2;&lt;br&gt;
&lt;br&gt;
to&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;percent_sparse = 0.001;&lt;br&gt;
&lt;br&gt;
and&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;percent_sparse += 0.1;&lt;br&gt;
&lt;br&gt;
to&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;percent_sparse += 0.001;&lt;br&gt;
&lt;br&gt;
And change your example to this:&lt;br&gt;
&lt;br&gt;
n = 1000; &lt;br&gt;
Sp16 = sprand(n,n,0.10);&lt;br&gt;
Sp16(Sp16&amp;gt;0) = 1./Sp16(Sp16&amp;gt;0);&lt;br&gt;
Sp16(Sp16&amp;gt;127) = 127;&lt;br&gt;
Sp16 = floor(Sp16);&lt;br&gt;
D16 = full(Sp16);&lt;br&gt;
D8 = int8(D16);&lt;br&gt;
Sp8 = int8tosparse(D8);&lt;br&gt;
whos&lt;br&gt;
isequal(D16,D8)&lt;br&gt;
isequal(Sp16,Sp8)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The percent_sparse changes are *only* for your particular example and are just a quick fix to get it working for this particular case. I will work on some more generic code later to handle arbitrary size matrices.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 14:46:02 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637261</link>
      <author>James Tursa</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in message &amp;lt;gqaqc5$l2k$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; And change your example to this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; n = 1000; &lt;br&gt;
&amp;gt; Sp16 = sprand(n,n,0.10);&lt;br&gt;
&amp;gt; Sp16(Sp16&amp;gt;0) = 1./Sp16(Sp16&amp;gt;0);&lt;br&gt;
&amp;gt; Sp16(Sp16&amp;gt;127) = 127;&lt;br&gt;
&amp;gt; Sp16 = floor(Sp16);&lt;br&gt;
&amp;gt; D16 = full(Sp16);&lt;br&gt;
&amp;gt; D8 = int8(D16);&lt;br&gt;
&amp;gt; Sp8 = int8tosparse(D8);&lt;br&gt;
&amp;gt; whos&lt;br&gt;
&amp;gt; isequal(D16,D8)&lt;br&gt;
&amp;gt; isequal(Sp16,Sp8)&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Oops .. typo holdover from my small test. Try this instead:&lt;br&gt;
&lt;br&gt;
n = 1000;&lt;br&gt;
Sp16 = sprand(n,n,0.001); % &amp;lt;-- changed 0.10 to 0.001&lt;br&gt;
Sp16(Sp16&amp;gt;0) = 1./Sp16(Sp16&amp;gt;0);&lt;br&gt;
Sp16(Sp16&amp;gt;127) = 127;&lt;br&gt;
Sp16 = floor(Sp16);&lt;br&gt;
D16 = full(Sp16);&lt;br&gt;
D8 = int8(D16);&lt;br&gt;
Sp8 = int8tosparse(D8);&lt;br&gt;
whos&lt;br&gt;
isequal(D16,D8)&lt;br&gt;
isequal(Sp16,Sp8)&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 16:03:02 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637290</link>
      <author>Derek O'Connor</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in message &amp;lt;gqarna$pv1$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in message &amp;lt;gqaqc5$l2k$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; And change your example to this:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; n = 1000; &lt;br&gt;
&amp;gt; &amp;gt; Sp16 = sprand(n,n,0.10);&lt;br&gt;
&amp;gt; &amp;gt; Sp16(Sp16&amp;gt;0) = 1./Sp16(Sp16&amp;gt;0);&lt;br&gt;
&amp;gt; &amp;gt; Sp16(Sp16&amp;gt;127) = 127;&lt;br&gt;
&amp;gt; &amp;gt; Sp16 = floor(Sp16);&lt;br&gt;
&amp;gt; &amp;gt; D16 = full(Sp16);&lt;br&gt;
&amp;gt; &amp;gt; D8 = int8(D16);&lt;br&gt;
&amp;gt; &amp;gt; Sp8 = int8tosparse(D8);&lt;br&gt;
&amp;gt; &amp;gt; whos&lt;br&gt;
&amp;gt; &amp;gt; isequal(D16,D8)&lt;br&gt;
&amp;gt; &amp;gt; isequal(Sp16,Sp8)&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Oops .. typo holdover from my small test. Try this instead:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; n = 1000;&lt;br&gt;
&amp;gt; Sp16 = sprand(n,n,0.001); % &amp;lt;-- changed 0.10 to 0.001&lt;br&gt;
&amp;gt; Sp16(Sp16&amp;gt;0) = 1./Sp16(Sp16&amp;gt;0);&lt;br&gt;
&amp;gt; Sp16(Sp16&amp;gt;127) = 127;&lt;br&gt;
&amp;gt; Sp16 = floor(Sp16);&lt;br&gt;
&amp;gt; D16 = full(Sp16);&lt;br&gt;
&amp;gt; D8 = int8(D16);&lt;br&gt;
&amp;gt; Sp8 = int8tosparse(D8);&lt;br&gt;
&amp;gt; whos&lt;br&gt;
&amp;gt; isequal(D16,D8)&lt;br&gt;
&amp;gt; isequal(Sp16,Sp8)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Dear John,&lt;br&gt;
&lt;br&gt;
Thanks for your very prompt reply. I made the changes you suggested and got this:&lt;br&gt;
&lt;br&gt;
---------------------------------------&lt;br&gt;
&amp;gt;&amp;gt; n = 1000;&lt;br&gt;
Sp16 = sprand(n,n,0.001); % &amp;lt;-- changed 0.10 to 0.001&lt;br&gt;
Sp16(Sp16&amp;gt;0) = 1./Sp16(Sp16&amp;gt;0);&lt;br&gt;
Sp16(Sp16&amp;gt;127) = 127;&lt;br&gt;
Sp16 = floor(Sp16);&lt;br&gt;
D16 = full(Sp16);&lt;br&gt;
D8 = int8(D16);&lt;br&gt;
Sp8 = int8tosparse(D8);&lt;br&gt;
&amp;gt;&amp;gt; whos&lt;br&gt;
&amp;nbsp;&amp;nbsp;Name         Size                    Bytes     Class     Attributes&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;D16       1000x1000            8000000  double&lt;br&gt;
&amp;nbsp;&amp;nbsp;D8         1000x1000            1000000  int8&lt;br&gt;
&amp;nbsp;&amp;nbsp;Sp16      1000x1000               24008  double    sparse&lt;br&gt;
&amp;nbsp;&amp;nbsp;Sp8       1000x1000                24008  double    sparse&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; isequal(D16,D8)&lt;br&gt;
isequal(Sp16,Sp8)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;br&gt;
--------------------------------------&lt;br&gt;
&lt;br&gt;
You can see that the size of Sp8 is the same as Sp16 so it&lt;br&gt;
seems there is no advantage in converting.&lt;br&gt;
&lt;br&gt;
Here is the type of problem I'm interested in:&lt;br&gt;
&lt;br&gt;
A 10^8 x 10^8 distance (adjacency) matrix with 20 non-zeros per row,&lt;br&gt;
and each non-zero is an integer in the range 1:127. Only addition&lt;br&gt;
and subtraction performed on these, and the values are never changed.&lt;br&gt;
This is typical of shortest path and network flow problems.&lt;br&gt;
&lt;br&gt;
Storage for these values is 20 x 10^8  = 8 x 20 x 10^8 = 16 GB in double.&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;&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;&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;&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;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;=   2 GB in int8.&lt;br&gt;
&lt;br&gt;
The storage for indexing information should be the same for both.&lt;br&gt;
&lt;br&gt;
Is there some fundamental difference between sparse matrices whose &lt;br&gt;
non-zeros are in the rang 1:127 and those in the range 10^(-308) to 10^(308) ?&lt;br&gt;
Or am I missing something here?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
&lt;br&gt;
Derek O'Connor</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 18:01:05 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637337</link>
      <author>James Tursa</author>
      <description>&quot;Derek O'Connor&quot; &amp;lt;derekroconnor@eircom.net&amp;gt; wrote in message &amp;lt;gqb07m$aau$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Dear John,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for your very prompt reply. I made the changes you suggested and got this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ---------------------------------------&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; n = 1000;&lt;br&gt;
&amp;gt; Sp16 = sprand(n,n,0.001); % &amp;lt;-- changed 0.10 to 0.001&lt;br&gt;
&amp;gt; Sp16(Sp16&amp;gt;0) = 1./Sp16(Sp16&amp;gt;0);&lt;br&gt;
&amp;gt; Sp16(Sp16&amp;gt;127) = 127;&lt;br&gt;
&amp;gt; Sp16 = floor(Sp16);&lt;br&gt;
&amp;gt; D16 = full(Sp16);&lt;br&gt;
&amp;gt; D8 = int8(D16);&lt;br&gt;
&amp;gt; Sp8 = int8tosparse(D8);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; whos&lt;br&gt;
&amp;gt;   Name         Size                    Bytes     Class     Attributes&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   D16       1000x1000            8000000  double&lt;br&gt;
&amp;gt;   D8         1000x1000            1000000  int8&lt;br&gt;
&amp;gt;   Sp16      1000x1000               24008  double    sparse&lt;br&gt;
&amp;gt;   Sp8       1000x1000                24008  double    sparse&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; isequal(D16,D8)&lt;br&gt;
&amp;gt; isequal(Sp16,Sp8)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;      1&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;      1&lt;br&gt;
&amp;gt; --------------------------------------&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You can see that the size of Sp8 is the same as Sp16 so it&lt;br&gt;
&amp;gt; seems there is no advantage in converting.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here is the type of problem I'm interested in:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A 10^8 x 10^8 distance (adjacency) matrix with 20 non-zeros per row,&lt;br&gt;
&amp;gt; and each non-zero is an integer in the range 1:127. Only addition&lt;br&gt;
&amp;gt; and subtraction performed on these, and the values are never changed.&lt;br&gt;
&amp;gt; This is typical of shortest path and network flow problems.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Storage for these values is 20 x 10^8  = 8 x 20 x 10^8 = 16 GB in double.&lt;br&gt;
&amp;gt;                                                                               =   2 GB in int8.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The storage for indexing information should be the same for both.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is there some fundamental difference between sparse matrices whose &lt;br&gt;
&amp;gt; non-zeros are in the rang 1:127 and those in the range 10^(-308) to 10^(308) ?&lt;br&gt;
&amp;gt; Or am I missing something here?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Yes, it seems you have a basic misunderstanding of what I have been doing. MATLAB only supports sparse matrices of double ... it does *not* support sparse matrices of any other class. The code I provided to you simply converts the full int8 input matrix to a double sparse matrix without first converting to a full double matrix like you did originally. The C code is simply a way to avoid creating the full double matrix intermediate step ... but the final results are the same regardless of what class you start with ... you will always end up with a *double* sparse matrix. There will be *no* storage savings on the final result. It appears you were expecting to have the fundamental storage class behind the scenes of int8tosparse be int8, but that is not the case. The only way to realize this would be to create a custom class that contained the int8 raw data and indexing and then write all of &lt;br&gt;
the sparse functions you need to support this custom class from scratch. That would be a huge undertaking. &lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 18:18:01 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637343</link>
      <author>James Tursa</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in message &amp;lt;gqb751$a7b$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ...MATLAB only supports sparse matrices of double ... it does *not* support sparse matrices of any other class...&lt;br&gt;
&lt;br&gt;
I meant for the operations you are interested in (addition, subtraction, etc.). MATLAB *does* of course support logical sparse matrices. Since the storage class behind the scenes for logical sparse matrices is 1-byte, I suppose this could be used as a starting point for a custom sparse int8 or uint8 class, and then write c-mex code to do the operations. But again, this would be quite a bit of work ...&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 24 Mar 2009 18:18:07 -0400</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#637344</link>
      <author>James Tursa</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; wrote in message &amp;lt;gqb751$a7b$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ...MATLAB only supports sparse matrices of double ... it does *not* support sparse matrices of any other class...&lt;br&gt;
&lt;br&gt;
I meant for the operations you are interested in (addition, subtraction, etc.). MATLAB *does* of course support logical sparse matrices. Since the storage class behind the scenes for logical sparse matrices is 1-byte, I suppose this could be used as a starting point for a custom sparse int8 or uint8 class, and then write c-mex code to do the operations. But again, this would be quite a bit of work ...&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Thu, 13 Nov 2008 15:30:20 -0500</pubDate>
      <title>Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#610713</link>
      <author>Bobane </author>
      <description>Hi,&lt;br&gt;
Am new to sparse matrices and hoping someone can help me with this. I have a large (150MB) binary file of 'int8' type data which I am loading into an array A. The data has a large number of 0's, so I would like to hold it in a sparse matrix. However, if I type the command B=sparse(A), I get the error message: &quot;??? Undefined function or method 'sparse' for input arguments of type 'int8'&quot;. I have read through the documentation on sparse matrices and can't see anywhere where it says one can only convert matrices of type 'double' to sparse (unless I missed it). In any case, is there any reason why it would not be possible to convert integer type arrays to sparse? Or am I doing something wrong?&lt;br&gt;
&lt;br&gt;
Thanks in advance for any suggestions.&lt;br&gt;
&lt;br&gt;
Bobane</description>
    </item>
    <item>
      <pubDate>Thu, 13 Nov 2008 15:33:02 -0500</pubDate>
      <title>Re: Convert array of class 'int8' to sparse</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239186#610716</link>
      <author>Matt </author>
      <description>&quot;Bobane&quot; &amp;lt;bobane73@hotmail.com&amp;gt; wrote in message &amp;lt;gfhh6c$dvr$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; Am new to sparse matrices and hoping someone can help me with this. I have a large (150MB) binary file of 'int8' type data which I am loading into an array A. The data has a large number of 0's, so I would like to hold it in a sparse matrix. However, if I type the command B=sparse(A), I get the error message: &quot;??? Undefined function or method 'sparse' for input arguments of type 'int8'&quot;. I have read through the documentation on sparse matrices and can't see anywhere where it says one can only convert matrices of type 'double' to sparse (unless I missed it). In any case, is there any reason why it would not be possible to convert integer type arrays to sparse? Or am I doing something wrong?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks in advance for any suggestions.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bobane&lt;br&gt;
&lt;br&gt;
The sparse data type does indeed only support doubles, much to the chagrin of many users. So all you can do is use double() to pre-convert your data.</description>
    </item>
  </channel>
</rss>

