Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: looking for faster way to build a big cell
Date: Mon, 15 Jul 2013 18:26:09 +0000 (UTC)
Organization: Boeing Co
Lines: 62
Message-ID: <ks1es1$rj2$1@newscl01ah.mathworks.com>
References: <ks072u$h3e$1@newscl01ah.mathworks.com> <ks0e9r$rqu$1@speranza.aioe.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-06-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1373912769 28258 172.30.248.38 (15 Jul 2013 18:26:09 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 15 Jul 2013 18:26:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:799342

"Nasser M. Abbasi" wrote in message <ks0e9r$rqu$1@speranza.aioe.org>...
> On 7/15/2013 2:07 AM, WK wrote:
> > hi all,
> >    I am looking for a faster way to generate a cell matrix. In my program,
> >I have a big matrix called MT of 10000x10000. There is another 1D vector
> >X of 1x5000. For each element of X, I am searching all element indices of
> >  MT such that the element is within +/- 0.5 around the current element of X. Here is what I did
> >
> > for n=1:5000
> >    P{n} = find( (MT>=X(n)-0.5) & (MT<=X(n)+0.5) );
> > end
> >
> > this loop is pretty slow. Is that any way to boost the code or
> >achieve the same thing without using the loop? Thanks.
> >
> 
> One thing you can try is reduce the amount of allocation inside
> the find by combining the test into one instead of two.
> 
> for n=1:N
>    P{n}=find( abs(MT-X(n))<=0.5 );
> end
> 
> 
> For example:
> -------------------------
> clear all
> MT=rand(5000);
> N=50;
> X=rand(N,1);
> 
> tic;
> for n=1:N
>    P{n}=find( abs(MT-X(n))<0.1 );
> end
> t=toc;
> fprintf('using abs check, time used = %f\n',t);
> clear P;
> 
> tic;
> for n=1:N
>    Z{n}=find(  (MT>=(X(n)-0.1)) & (MT<=(X(n)+0.1)) );
> end
> t=toc;
> fprintf('using double checks, time used = %f\n',t);
> -------------------------
> 
> using abs check, time used = 7.053266
> using double checks, time used = 22.197705
> 
> 
> There might be other things to do to speed it up.
> 
> --Nasser

Not sure it will make a significant difference to the timing, but you should pre-allocate the cell array result. e.g.,

P = cell(N,1);
    :
Z = cell(N,1);

James Tursa