Path: news.mathworks.com!newsfeed-00.mathworks.com!news.kjsl.com!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!aioe.org!.POSTED!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 04:10:21 -0500
Organization: Aioe.org NNTP Server
Lines: 53
Message-ID: <ks0e9r$rqu$1@speranza.aioe.org>
References: <ks072u$h3e$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: RkGzD5HVj3fmwvnC3F06ww.user.speranza.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
X-Notice: Filtered by postfilter v. 0.8.2
Xref: news.mathworks.com comp.soft-sys.matlab:799315

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