<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633</link>
    <title>MATLAB Central Newsreader - vectorize finding upper envelope of vector</title>
    <description>Feed for thread: vectorize finding upper envelope of vector</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 13 Mar 2008 18:30:21 -0400</pubDate>
      <title>vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420766</link>
      <author>ashipyard </author>
      <description>I hope someone can help me vectorize some code.&lt;br&gt;
&lt;br&gt;
I have a vector a. I want to return the upper envelope of a,&lt;br&gt;
based on its original sort order.&lt;br&gt;
&lt;br&gt;
E.g.&lt;br&gt;
&lt;br&gt;
if a = [2 3 4 2 7 5 8]&lt;br&gt;
&lt;br&gt;
I would want to return: [2 3 4 4 7 7 8].&lt;br&gt;
&lt;br&gt;
Clearly this is very easy in a for loop.&lt;br&gt;
&lt;br&gt;
for i=2:length(a)&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (a(i)&amp;lt;a(i-1))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a(i)=a(i-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
However, I can't think of an easy way to vectorize this.&lt;br&gt;
Looping is slow when the length of a is large.&lt;br&gt;
&lt;br&gt;
Thank you for your help</description>
    </item>
    <item>
      <pubDate>Thu, 13 Mar 2008 19:31:57 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420778</link>
      <author>Yuri Geshelin</author>
      <description>&quot;ashipyard &quot; &amp;lt;andrewshipyard@hush.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;frbrrt$n43$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I hope someone can help me vectorize some code.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a vector a. I want to return the upper envelope of &lt;br&gt;
a,&lt;br&gt;
&amp;gt; based on its original sort order.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; E.g.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if a = [2 3 4 2 7 5 8]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I would want to return: [2 3 4 4 7 7 8].&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Clearly this is very easy in a for loop.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=2:length(a)&lt;br&gt;
&amp;gt;   if (a(i)&amp;lt;a(i-1))&lt;br&gt;
&amp;gt;     a(i)=a(i-1);&lt;br&gt;
&amp;gt;   end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, I can't think of an easy way to vectorize this.&lt;br&gt;
&amp;gt; Looping is slow when the length of a is large.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you for your help&lt;br&gt;
&lt;br&gt;
ifi = find(diff(a)&amp;lt;0)&lt;br&gt;
a(ifi+1)=a(ifi)</description>
    </item>
    <item>
      <pubDate>Thu, 13 Mar 2008 21:29:02 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420800</link>
      <author>ashipyard </author>
      <description>&quot;Yuri Geshelin&quot; &amp;lt;geshelin@hotmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;frbvfd$bj4$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; ifi = find(diff(a)&amp;lt;0)&lt;br&gt;
&amp;gt; a(ifi+1)=a(ifi)&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Thanks for your reply, however this doesn't work how I want.&lt;br&gt;
Perhaps I didn't explain my problem clearly enough.&lt;br&gt;
&lt;br&gt;
I would like each element to be the highest from all&lt;br&gt;
previous elements. &lt;br&gt;
&lt;br&gt;
So, if if &lt;br&gt;
&lt;br&gt;
a=[1 2 3 4 3 2 3 5]&lt;br&gt;
&lt;br&gt;
then I would like this to become&lt;br&gt;
&lt;br&gt;
[1 2 3 4 4 4 4 5].&lt;br&gt;
&lt;br&gt;
Using the code you provided, this would yield&lt;br&gt;
&lt;br&gt;
[1 2 3 4 4 3 3 5] which isn't what I after.</description>
    </item>
    <item>
      <pubDate>Thu, 13 Mar 2008 22:58:01 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420809</link>
      <author>Dan Haeg</author>
      <description>&quot;ashipyard &quot; &amp;lt;andrewshipyard@hush.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;frc6au$ehp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Yuri Geshelin&quot; &amp;lt;geshelin@hotmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;gt; &amp;lt;frbvfd$bj4$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; ifi = find(diff(a)&amp;lt;0)&lt;br&gt;
&amp;gt; &amp;gt; a(ifi+1)=a(ifi)&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for your reply, however this doesn't work how I want.&lt;br&gt;
&amp;gt; Perhaps I didn't explain my problem clearly enough.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I would like each element to be the highest from all&lt;br&gt;
&amp;gt; previous elements. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So, if if &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a=[1 2 3 4 3 2 3 5]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; then I would like this to become&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [1 2 3 4 4 4 4 5].&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Using the code you provided, this would yield&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [1 2 3 4 4 3 3 5] which isn't what I after.&lt;br&gt;
&lt;br&gt;
this may run fast enough.&lt;br&gt;
&lt;br&gt;
a=[1 2 3 4 3 2 3 5]&lt;br&gt;
ifi = find(diff(a)&amp;lt;0)&lt;br&gt;
while ~isempty(ifi)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a(ifi+1)=a(ifi)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ifi = find(diff(a)&amp;lt;0)&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Thu, 13 Mar 2008 23:07:02 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420810</link>
      <author>Roger Stafford</author>
      <description>&quot;ashipyard &quot; &amp;lt;andrewshipyard@hush.com&amp;gt; wrote in message &amp;lt;frbrrt$n43&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I hope someone can help me vectorize some code.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a vector a. I want to return the upper envelope of a,&lt;br&gt;
&amp;gt; based on its original sort order.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; E.g.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if a = [2 3 4 2 7 5 8]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I would want to return: [2 3 4 4 7 7 8].&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Clearly this is very easy in a for loop.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=2:length(a)&lt;br&gt;
&amp;gt;   if (a(i)&amp;lt;a(i-1))&lt;br&gt;
&amp;gt;     a(i)=a(i-1);&lt;br&gt;
&amp;gt;   end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, I can't think of an easy way to vectorize this.&lt;br&gt;
&amp;gt; Looping is slow when the length of a is large.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you for your help&lt;br&gt;
----------&lt;br&gt;
&amp;nbsp;&amp;nbsp;What you are asking for is a &quot;cumulative&quot; maximum function.  I have seen &lt;br&gt;
this question arise many times on this newsgroup and each time no-one &lt;br&gt;
seems to have been able to come up with a good vectorized solution.  &lt;br&gt;
Furthermore, even if someone did manage to stumble across some magical &lt;br&gt;
combination of such things as cumsums, diffs, and whatnot, that would &lt;br&gt;
accomplish the task, it is highly likely that, at least on the newer matlab &lt;br&gt;
versions, it would take far longer to execute than doing it your for-loop way.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I strongly suspect that it would be comparatively easy for Mathworks, if they &lt;br&gt;
thought it was worthwhile, to devise a fast cumulative maximum function at &lt;br&gt;
the low level coding which is available to them and which would execute with &lt;br&gt;
the same speed as 'sum', 'find', etc., but until and unless they do, you may &lt;br&gt;
have to be content with the for-loop method.  There is no disgrace in using a &lt;br&gt;
for-loop.  It is a highly essential construct in the matlab language.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Remember, each of the so-called vectorized operations is accomplished in &lt;br&gt;
the same way it would be done in a for-loop or the like, one step at a time.  &lt;br&gt;
To do 'sum', the computer adds the first number to the second, then adds &lt;br&gt;
their sum to the third, then that sum to the fourth one, etc., just as a for-loop &lt;br&gt;
would do.  However, doing this at low level coding can eliminate a certain &lt;br&gt;
amount of overhead that is associated with for-loop indexing executed at a &lt;br&gt;
higher language level.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 15:47:01 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420922</link>
      <author>Jos </author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt;&lt;br&gt;
wrote in message &amp;lt;frcc2m$h6i$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;ashipyard &quot; &amp;lt;andrewshipyard@hush.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;frbrrt$n43&lt;br&gt;
&amp;gt; $1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I hope someone can help me vectorize some code.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have a vector a. I want to return the upper envelope of a,&lt;br&gt;
&amp;gt; &amp;gt; based on its original sort order.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; E.g.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; if a = [2 3 4 2 7 5 8]&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I would want to return: [2 3 4 4 7 7 8].&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Clearly this is very easy in a for loop.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for i=2:length(a)&lt;br&gt;
&amp;gt; &amp;gt;   if (a(i)&amp;lt;a(i-1))&lt;br&gt;
&amp;gt; &amp;gt;     a(i)=a(i-1);&lt;br&gt;
&amp;gt; &amp;gt;   end&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; However, I can't think of an easy way to vectorize this.&lt;br&gt;
&amp;gt; &amp;gt; Looping is slow when the length of a is large.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thank you for your help&lt;br&gt;
&amp;gt; ----------&lt;br&gt;
&amp;gt;   What you are asking for is a &quot;cumulative&quot; maximum&lt;br&gt;
function.  I have seen &lt;br&gt;
&amp;gt; this question arise many times on this newsgroup and each&lt;br&gt;
time no-one &lt;br&gt;
&amp;gt; seems to have been able to come up with a good vectorized&lt;br&gt;
solution.  &lt;br&gt;
&amp;gt; Furthermore, even if someone did manage to stumble across&lt;br&gt;
some magical &lt;br&gt;
&amp;gt; combination of such things as cumsums, diffs, and whatnot,&lt;br&gt;
that would &lt;br&gt;
&amp;gt; accomplish the task, it is highly likely that, at least on&lt;br&gt;
the newer matlab &lt;br&gt;
&amp;gt; versions, it would take far longer to execute than doing&lt;br&gt;
it your for-loop way.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   I strongly suspect that it would be comparatively easy&lt;br&gt;
for Mathworks, if they &lt;br&gt;
&amp;gt; thought it was worthwhile, to devise a fast cumulative&lt;br&gt;
maximum function at &lt;br&gt;
&amp;gt; the low level coding which is available to them and which&lt;br&gt;
would execute with &lt;br&gt;
&amp;gt; the same speed as 'sum', 'find', etc., but until and&lt;br&gt;
unless they do, you may &lt;br&gt;
&amp;gt; have to be content with the for-loop method.  There is no&lt;br&gt;
disgrace in using a &lt;br&gt;
&amp;gt; for-loop.  It is a highly essential construct in the&lt;br&gt;
matlab language.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Remember, each of the so-called vectorized operations is&lt;br&gt;
accomplished in &lt;br&gt;
&amp;gt; the same way it would be done in a for-loop or the like,&lt;br&gt;
one step at a time.  &lt;br&gt;
&amp;gt; To do 'sum', the computer adds the first number to the&lt;br&gt;
second, then adds &lt;br&gt;
&amp;gt; their sum to the third, then that sum to the fourth one,&lt;br&gt;
etc., just as a for-loop &lt;br&gt;
&amp;gt; would do.  However, doing this at low level coding can&lt;br&gt;
eliminate a certain &lt;br&gt;
&amp;gt; amount of overhead that is associated with for-loop&lt;br&gt;
indexing executed at a &lt;br&gt;
&amp;gt; higher language level.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
A one-line vectorized cumulative maximum function:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;a = [2 3 4 2 7 5 8] % row vector&lt;br&gt;
&amp;nbsp;&amp;nbsp;CMA = max(triu(toeplitz(a)))&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;% -&amp;gt; CMA =  2  3  4  4  7   7   8&lt;br&gt;
&lt;br&gt;
which, however(!), will create an intermediate (numel(A).^2)&lt;br&gt;
matrix that may cause memory troubles for long vectors.&lt;br&gt;
&lt;br&gt;
SLIDEFUN on the File Exchange may be of interest to you as&lt;br&gt;
well (shameful self promotion ...).&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 15:59:33 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420925</link>
      <author>Arthur G</author>
      <description>On 2008-03-14 11:47:01 -0400, &quot;Jos &quot; &amp;lt;DELjos@jasenDEL.nl&amp;gt; said:&lt;br&gt;
&amp;gt; A one-line vectorized cumulative maximum function:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   a = [2 3 4 2 7 5 8] % row vector&lt;br&gt;
&amp;gt;   CMA = max(triu(toeplitz(a)))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   % -&amp;gt; CMA =  2  3  4  4  7   7   8&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; which, however(!), will create an intermediate (numel(A).^2)&lt;br&gt;
&amp;gt; matrix that may cause memory troubles for long vectors.&lt;br&gt;
&lt;br&gt;
Another caveat: the above one-liner won't work if the vector starts &lt;br&gt;
with any negative numbers.</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 17:29:02 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420949</link>
      <author>Roger Stafford</author>
      <description>&quot;Jos &quot; &amp;lt;DELjos@jasenDEL.nl&amp;gt; wrote in message &amp;lt;fre6ll$p28&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; A one-line vectorized cumulative maximum function:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   a = [2 3 4 2 7 5 8] % row vector&lt;br&gt;
&amp;gt;   CMA = max(triu(toeplitz(a)))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   % -&amp;gt; CMA =  2  3  4  4  7   7   8&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; which, however(!), will create an intermediate (numel(A).^2)&lt;br&gt;
&amp;gt; matrix that may cause memory troubles for long vectors.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; SLIDEFUN on the File Exchange may be of interest to you as&lt;br&gt;
&amp;gt; well (shameful self promotion ...).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; hth&lt;br&gt;
&amp;gt; Jos&lt;br&gt;
-------&lt;br&gt;
&amp;nbsp;&amp;nbsp;Thanks Jos.  I stand corrected.  I should have done more thorough searching &lt;br&gt;
before making my claim.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;However, I do worry about its execution speed since it is an order n^2 &lt;br&gt;
algorithm rather than the order n of the OP's for-loop method.  On my &lt;br&gt;
ancient matlab version it runs about three times as long as the for-loop, even &lt;br&gt;
for a 32-element vector.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 19:13:03 -0400</pubDate>
      <title>Re: vectorize finding upper envelope of vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165633#420966</link>
      <author>Roger Stafford</author>
      <description>Arthur G &amp;lt;gorramfreak+news@gmail.com&amp;gt; wrote in message &amp;lt;47daa0e5$0&lt;br&gt;
$307$b45e6eb0@senator-bedfellow.mit.edu&amp;gt;...&lt;br&gt;
&amp;gt; On 2008-03-14 11:47:01 -0400, &quot;Jos &quot; &amp;lt;DELjos@jasenDEL.nl&amp;gt; said:&lt;br&gt;
&amp;gt; &amp;gt; A one-line vectorized cumulative maximum function:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;   a = [2 3 4 2 7 5 8] % row vector&lt;br&gt;
&amp;gt; &amp;gt;   CMA = max(triu(toeplitz(a)))&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;   % -&amp;gt; CMA =  2  3  4  4  7   7   8&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; which, however(!), will create an intermediate (numel(A).^2)&lt;br&gt;
&amp;gt; &amp;gt; matrix that may cause memory troubles for long vectors.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Another caveat: the above one-liner won't work if the vector starts &lt;br&gt;
&amp;gt; with any negative numbers.&lt;br&gt;
-------&lt;br&gt;
&amp;nbsp;&amp;nbsp;Jos could easily correct for that with:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;CMA = max(toeplitz(repmat(a(1),size(a)),a))&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
  </channel>
</rss>

