Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!25g2000hsx.googlegroups.com!not-for-mail
From: heiko_marx@hotmail.com
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorizing data lookback?
Date: Mon, 1 Sep 2008 02:26:39 -0700 (PDT)
Organization: http://groups.google.com
Lines: 46
Message-ID: <d3e32ef1-ba0b-4789-a5b1-220c64ee361c@25g2000hsx.googlegroups.com>
References: <g9db32$oq6$1@fred.mathworks.com>
NNTP-Posting-Host: 87.139.31.42
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1220261199 16033 127.0.0.1 (1 Sep 2008 09:26:39 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 1 Sep 2008 09:26:39 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: 25g2000hsx.googlegroups.com; posting-host=87.139.31.42; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.1) 
X-HTTP-Via: 1.1 tux.sinus:3128 (Squid/2.2.STABLE5)
Xref: news.mathworks.com comp.soft-sys.matlab:488081



On 31 Aug., 07:44, "Nicholas "
<OtherPeopleTookAllTheGoodNa...@deleteme.gmail.com> wrote:
> I'm trying to develop a function that traverses a vector and
> outputs a '1' if the current value is greater than the
> previous N-values, '0' if it's not.
>
> Programming this into a "for" loop is trivial:
>
> for ii =3D n:length(data)
>
> =A0 =A0 =A0if data(ii) > max( =A0data(ii-n:ii-1) =A0);
> =A0 =A0 =A0 =A0 =A0 output(ii) =3D 1;
> =A0 =A0 =A0else
> =A0 =A0 =A0 =A0 =A0 output(ii) =3D 0;
>
> end;
>
> The problem is this approach takes forever and a day to
> process. It takes ~2 orders of magnitude longer to run than
> similar (but vectorized) functions I've written that operate
> on the same exact data.
>
> I've been trying to vectorize this function as well to
> improve the speed, but I can't wrap my brain around it. Any
> suggestions?

Hi.

I found that avoiding the max function in each loop reduces the time
by a good amount.

>> tic; for ii =3D n:length(data), if data(ii) > max(data(1:ii-1)); output(=
ii) =3D 1; else output(ii) =3D 0;end, end, toc
Elapsed time is 0.010647 seconds.
>> tic; m =3D max(data(1:n-1)); for ii =3D n:length(data), if data(ii) > m,=
 output2(ii) =3D 1; m =3D data(ii); else output2(ii) =3D 0;end, end, toc
Elapsed time is 0.001932 seconds.
>> isequal(output, output2)
ans =3D
     1

The times refer to a data set of 1000 random values, aand are of
course machine-dependent.

Hope this helps,
Heiko