Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Index matrix to sparse matrix... saving memory
Date: Tue, 8 Jun 2010 20:25:20 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 123
Message-ID: <hum8vg$65s$1@fred.mathworks.com>
References: <htrfev$fo7$1@fred.mathworks.com> <htrk32$2vm$1@fred.mathworks.com> <htrlad$in3$1@fred.mathworks.com> <htrmbl$nn6$1@fred.mathworks.com> <hts2hl$3tk$1@fred.mathworks.com> <htt9rq$1s8$1@fred.mathworks.com> <htthum$sg$1@fred.mathworks.com> <httk3s$hb0$1@fred.mathworks.com> <httl5l$ns3$1@fred.mathworks.com> <hum6no$7rt$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1276028720 6332 172.30.248.37 (8 Jun 2010 20:25:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 8 Jun 2010 20:25:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:643238

First, this a an month-old thread. I'm sorry but if you haven't not pay enough attention to what has been suggested for month (including Matt's remark about the miss-use of sparse), the a lot time is waste.

Now it will be the last time to write something on this topic, then I'll move on. Here we go:

First I change your original function by removing the garbage inside, and also change one comparison test (from > to >=) to match HISTC.

%%
function [ index_range ] = range_bin_sparse( r_4d, bin_size )
%RANGE_BIN Identifies the index values in r_4d that are within a bin of a given size
% each radar is done individually and the ouput file is a sparse
% matrix
% [ range_index ] = range_bin_sparse( r_4d, bin_size )

%single range bin binning of a single radar
r_bin_max = ceil((((max(r_4d(:)))))/bin_size);
r_bin = (bin_size/2:bin_size:r_bin_max*bin_size+bin_size/2);%[m]
index_range = zeros(length(r_bin),250);

%filter variables
rH = r_bin + bin_size/2;
rL = r_bin - bin_size/2;
%find exceptable values for each range bin
for v=1:length(r_bin)
    index_temp = find((r_4d) >= rL(v) & (r_4d) <rH(v)); % change here
    index_range(v,1:length(index_temp)) = index_temp;
end
index_range = sparse(index_range');

end

% Now here is the data I use to test
r_4d=peaks(50)+7; % + 7 to make data positive
bin_size = 0.1;
[ index_range ] = range_bin_sparse( r_4d, bin_size );

% Here is a way to use HISTC
nbins = ceil(max(r_4d(:))/bin_size);
edges = (0:nbins)*bin_size;
[~,bin] = histc(r_4d,edges);
bool = sparse(1:numel(bin), bin,true);

% Note the the logical sparse matrix BOOL is not exactly like range, but it contains the
% same information, if you want to find which index on the 50th bins, you can do this:

>> index_range(:,50)

ans =

   (1,1)            525
   (2,1)            526
   (3,1)            527
   (4,1)            528
   (5,1)            580
   (6,1)            826
   (7,1)            830
   (8,1)           1013
   (9,1)           1064
  (10,1)           1206
  (11,1)           1406
  (12,1)           1517

% Or this:

>> find(bool(:,50))

ans =

         525
         526
         527
         528
         580
         826
         830
        1013
        1064
        1206
        1406
        1517

% You see that they contain the same thing.

In practice, next calculation does not need the FIND command, because BOOL can be used as matrix for LOGICAL indexing, that's what Matt suggest you. For example figure out values of thet array r_4d() falling the 50th bins

>> r_4d(bool(:,50)) 

ans =

    4.9948
    4.9059
    4.9003
    4.9737
    4.9775
    4.9397
    4.9658
    4.9965
    4.9447
    4.9068
    4.9602
    4.9365

% Check if they fall inside the bins

 >> r_4d(bool(:,50))>=edges(50) & r_4d(bool(:,50))<edges(51)

ans =

     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1

>>

% Bruno