Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorizing data lookback?
Date: Wed, 3 Sep 2008 21:38:02 +0000 (UTC)
Organization: Lunds Universitet
Lines: 62
Message-ID: <g9n03q$3le$1@fred.mathworks.com>
References: <g9db32$oq6$1@fred.mathworks.com> <g9mng6$hbc$1@fred.mathworks.com> <g9mppq$f7c$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1220477882 3758 172.30.248.35 (3 Sep 2008 21:38:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 3 Sep 2008 21:38:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 5707
Xref: news.mathworks.com comp.soft-sys.matlab:488492



"Bruno Luong" <b.luong@fogale.findmycountry> wrote in
message <g9mppq$f7c$1@fred.mathworks.com>...
> Faster still:
> .....

and here is a third alternative that clocks in favourably

% clean up and start test from scratch
clear all
pack

%some test data
ndata=10e6;  %%% some more data, may need to reduced
nwin=5;
nwin2=floor(nwin/2);
upper=10;
data=round(upper*rand(1,ndata));

% Bruno 1
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 


% Bruno 2  runs out of memory
%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 


% running Extreme: FEX #18551 by Dan Kominsky
tic
nwin2=floor(nwin/2); % for convenience
maxd=runningExtreme(data,nwin,'max'); 
% adjustments to handle centred running window
maxd(1:nwin2)=NaN;
maxd=[repmat(NaN,1,nwin2+1) maxd(1:end-nwin2-1)];
output3=data>maxd;
toc

%all(output1==output2)

all(output1==output3)

%%%%%


hth
Lars