Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorize finding upper envelope of vector
Date: Thu, 13 Mar 2008 19:31:57 +0000 (UTC)
Organization: Bedford Institute of Oceanography
Lines: 30
Message-ID: <frbvfd$bj4$1@fred.mathworks.com>
References: <frbrrt$n43$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 1205436717 11876 172.30.248.35 (13 Mar 2008 19:31:57 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 13 Mar 2008 19:31:57 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1149396
Xref: news.mathworks.com comp.soft-sys.matlab:457116



"ashipyard " <andrewshipyard@hush.com> wrote in message 
<frbrrt$n43$1@fred.mathworks.com>...
> I hope someone can help me vectorize some code.
> 
> I have a vector a. I want to return the upper envelope of 
a,
> based on its original sort order.
> 
> E.g.
> 
> if a = [2 3 4 2 7 5 8]
> 
> I would want to return: [2 3 4 4 7 7 8].
> 
> Clearly this is very easy in a for loop.
> 
> for i=2:length(a)
>   if (a(i)<a(i-1))
>     a(i)=a(i-1);
>   end
> end
> 
> However, I can't think of an easy way to vectorize this.
> Looping is slow when the length of a is large.
> 
> Thank you for your help

ifi = find(diff(a)<0)
a(ifi+1)=a(ifi)