Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorizing data lookback?
Date: Wed, 3 Sep 2008 19:50:18 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 31
Message-ID: <g9mppq$f7c$1@fred.mathworks.com>
References: <g9db32$oq6$1@fred.mathworks.com> <g9mng6$hbc$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="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1220471418 15596 172.30.248.37 (3 Sep 2008 19:50:18 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 3 Sep 2008 19:50:18 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:488472



Faster still:

ndata=750e3;
nwin=25;
upper=10;

data=upper*rand(1,ndata);

tic
output1=zeros(size(data));
for ii = nwin+1:length(data)
     if data(ii) > max( data(ii-nwin:ii-1) );
          output1(ii) = 1;
     else
          output1(ii) = 0;
     end
end
toc % Elapsed time is 9.467494 seconds.

tic
temp = data(max(bsxfun(@minus, (1:ndata),(1:nwin)'),1));
for i=1:size(temp,1)
    temp(i,1:i)=Inf;
end
output2=all(bsxfun(@gt,data,temp),1);
toc % Elapsed time is 1.530637 seconds.

all(output1==output2)

% Bruno