Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: vectorize finding upper envelope of vector
Date: Thu, 13 Mar 2008 18:30:21 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 23
Message-ID: <frbrrt$n43$1@fred.mathworks.com>
Reply-To: <HIDDEN>
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 1205433021 23683 172.30.248.37 (13 Mar 2008 18:30:21 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 13 Mar 2008 18:30:21 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1173724
Xref: news.mathworks.com comp.soft-sys.matlab:457104



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