Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Convert array of class 'int8' to sparse
Date: Tue, 24 Mar 2009 14:23:01 +0000 (UTC)
Organization: Boeing
Lines: 71
Message-ID: <gqaqc5$l2k$1@fred.mathworks.com>
References: <gfhh6c$dvr$1@fred.mathworks.com> <gq9bg5$3ra$1@fred.mathworks.com> <gqa8th$7j0$1@fred.mathworks.com> <gqae67$vg$1@fred.mathworks.com>
Reply-To: <HIDDEN>
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 1237904581 21588 172.30.248.37 (24 Mar 2009 14:23:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 24 Mar 2009 14:23:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:527261


"Derek O'Connor" <derekroconnor@eircom.net> wrote in message <gqae67$vg$1@fred.mathworks.com>...
> 
> I then ran
> 
> --------------------------------------------------------
> >> n = 1000; Sp16 = sprand(n,n,0.001); D16 = full(Sp16);...
>                D8 = int8(D16); Sp8 = int8tosparse(D8);...
>                whos
> 
>   Name         Size                       Bytes  Class       Attributes
> 
> 
>   Sp16     1000x1000                24,008   double    sparse
>   D16      1000x1000            8,000,000   double
>   D8        1000x1000            1,000,000   int8
>   Sp8       1000x1000           3,208,008   double    sparse
>   n           1x1                     8  double
> 
> commas added so I can read the sizes
> -----------------------------------------------------------
> 
> I know nothing about C/C++ programming and compiling. Any suggestions?
> 
> A complete mex function that works for *all* integer classes would be very useful, especially when working with large sparse graphs.
> 
> Regards,
> 
> Derek O'Connor

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:

int j,k,m,n,nzmax,*irs,*jcs,cmplx,isfull;

to

mwSize j,k,m,n,nzmax,*irs,*jcs,cmplx,isfull;

and

  percent_sparse = 0.2;

to

  percent_sparse = 0.001;

and

          percent_sparse += 0.1;

to

          percent_sparse += 0.001;

And change your example to this:

n = 1000; 
Sp16 = sprand(n,n,0.10);
Sp16(Sp16>0) = 1./Sp16(Sp16>0);
Sp16(Sp16>127) = 127;
Sp16 = floor(Sp16);
D16 = full(Sp16);
D8 = int8(D16);
Sp8 = int8tosparse(D8);
whos
isequal(D16,D8)
isequal(Sp16,Sp8)


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.

James Tursa