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 18:01:05 +0000 (UTC)
Organization: Boeing
Lines: 55
Message-ID: <gqb751$a7b$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> <gqaqc5$l2k$1@fred.mathworks.com> <gqarna$pv1$1@fred.mathworks.com> <gqb07m$aau$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1237917665 10475 172.30.248.35 (24 Mar 2009 18:01:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 24 Mar 2009 18:01:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:527344


"Derek O'Connor" <derekroconnor@eircom.net> wrote in message <gqb07m$aau$1@fred.mathworks.com>...
> 
> Dear John,
> 
> Thanks for your very prompt reply. I made the changes you suggested and got this:
> 
> ---------------------------------------
> >> n = 1000;
> Sp16 = sprand(n,n,0.001); % <-- changed 0.10 to 0.001
> Sp16(Sp16>0) = 1./Sp16(Sp16>0);
> Sp16(Sp16>127) = 127;
> Sp16 = floor(Sp16);
> D16 = full(Sp16);
> D8 = int8(D16);
> Sp8 = int8tosparse(D8);
> >> whos
>   Name         Size                    Bytes     Class     Attributes
> 
>   D16       1000x1000            8000000  double
>   D8         1000x1000            1000000  int8
>   Sp16      1000x1000               24008  double    sparse
>   Sp8       1000x1000                24008  double    sparse
> 
> >> isequal(D16,D8)
> isequal(Sp16,Sp8)
> ans =
>      1
> ans =
>      1
> --------------------------------------
> 
> You can see that the size of Sp8 is the same as Sp16 so it
> seems there is no advantage in converting.
> 
> Here is the type of problem I'm interested in:
> 
> A 10^8 x 10^8 distance (adjacency) matrix with 20 non-zeros per row,
> and each non-zero is an integer in the range 1:127. Only addition
> and subtraction performed on these, and the values are never changed.
> This is typical of shortest path and network flow problems.
> 
> Storage for these values is 20 x 10^8  = 8 x 20 x 10^8 = 16 GB in double.
>                                                                               =   2 GB in int8.
> 
> The storage for indexing information should be the same for both.
> 
> Is there some fundamental difference between sparse matrices whose 
> non-zeros are in the rang 1:127 and those in the range 10^(-308) to 10^(308) ?
> Or am I missing something here?
> 

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 
the sparse functions you need to support this custom class from scratch. That would be a huge undertaking. 

James Tursa