Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: filtering data problem
Date: Thu, 23 Apr 2009 06:30:07 +0000 (UTC)
Organization: Erasmus MC
Lines: 29
Message-ID: <gsp1tf$bno$1@fred.mathworks.com>
References: <gsoc01$eog$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 1240468207 12024 172.30.248.37 (23 Apr 2009 06:30:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 23 Apr 2009 06:30:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:534863


"Mike " <sulfateion@gmail.com> wrote in message <gsoc01$eog$1@fred.mathworks.com>...
> Hi
>     I usually need to filter data.  Sometimes I use logical-indexing, but I found there are many temporary arrays I created.  Does that way waste memory?  How to avoid this?
> 
> Mike

Logical indexing is very powerful but indeed creates a (temporary) array. However, they use 1/8 of the memory occupied by  double array of the same size:
A = rand(100,1) ; B = A > 0.5 ; whos

You can clear out, or overwrite, any variables you no longer need:
A = rand(10,1) ;
q = A > 0.5 ;
A = A(q) ; % overwrite
clear q ;

If this is not an option, you can turn to very inefficient loops, like this one, which does not occupy (a lot of) additional memory ... 

A = rand(10,1) ;
idx = 1 ;
while (idx <= numel(A)),
  if A(idx) > 0.5,
    idx = idx + 1 ; % goto next element
  else
    A(idx) = [] ; % remove element from list
  end
end

hth
Jos