<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/310547</link>
    <title>MATLAB Central Newsreader - vectorization suggestion</title>
    <description>Feed for thread: vectorization suggestion</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 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.es/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 16 Jul 2011 22:04:08 +0000</pubDate>
      <title>vectorization suggestion</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/310547#845681</link>
      <author>Piero Lanucara</author>
      <description>Hi to all,&lt;br&gt;
There exist a simple and fast way to do this:&lt;br&gt;
&lt;br&gt;
v_orig=[1 0 2 0 3 0 4 0 0 5 0 0 6] in&lt;br&gt;
v_end=[1 2 2 3 3 4 4 5 5 5 6 6 6]&lt;br&gt;
&lt;br&gt;
in the end  the 0 values has to be replace with  the first non zero element shifting towards the right direction....of course this vector is only an example because I use very huge vector like this!&lt;br&gt;
thank's in advance&lt;br&gt;
Piero</description>
    </item>
    <item>
      <pubDate>Sun, 17 Jul 2011 00:13:08 +0000</pubDate>
      <title>Re: vectorization suggestion</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/310547#845688</link>
      <author>Roger Stafford</author>
      <description>"Piero Lanucara" &amp;lt;lanucara@caspur.it&amp;gt; wrote in message &amp;lt;ivt1so$1uu$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi to all,&lt;br&gt;
&amp;gt; There exist a simple and fast way to do this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; v_orig=[1 0 2 0 3 0 4 0 0 5 0 0 6] in&lt;br&gt;
&amp;gt; v_end=[1 2 2 3 3 4 4 5 5 5 6 6 6]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; in the end  the 0 values has to be replace with  the first non zero element shifting towards the right direction....of course this vector is only an example because I use very huge vector like this!&lt;br&gt;
&amp;gt; thank's in advance&lt;br&gt;
&amp;gt; Piero&lt;br&gt;
- - - - - - - - - -&lt;br&gt;
&amp;nbsp;&amp;nbsp;What's wrong with just using a simple for-loop?  It may be about as efficient as any vectorization and is a lot easier to code.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;v_end = v_orig;&lt;br&gt;
&amp;nbsp;t = 0;&lt;br&gt;
&amp;nbsp;for k = length(v_end):-1:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;if v_end(k) == 0, v_end(k) = t; else, t = v_end(k); end&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sun, 17 Jul 2011 00:40:13 +0000</pubDate>
      <title>Re: vectorization suggestion</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/310547#845689</link>
      <author>Matt J </author>
      <description>"Roger Stafford" wrote in message &amp;lt;ivt9ek$jq3$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;   What's wrong with just using a simple for-loop?  It may be about as efficient as any vectorization and is a lot easier to code.&lt;br&gt;
&amp;gt;&lt;br&gt;
=========================&lt;br&gt;
&lt;br&gt;
I agree that this might be a case of vectorization overkill. Nevertheless, the following might be a better alternative to a for-loop if the strings of zeros are all fairly short&lt;br&gt;
&lt;br&gt;
v_end=v_orig;&lt;br&gt;
idx=1;&lt;br&gt;
while any(idx)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;s = circshift(v_end,[0,-1]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;idx=s &amp; ~v_end;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;v_end(idx)=s(idx);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Sun, 17 Jul 2011 07:44:08 +0000</pubDate>
      <title>Re: vectorization suggestion</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/310547#845699</link>
      <author>Piero Lanucara</author>
      <description>many thank's for the suggestions&lt;br&gt;
I agree that probably the first implentation is faster with huge (the dimension is more or less the half number of non zeros of large symmetric sparse) matrix&lt;br&gt;
probably there's some chance to do calculation in place so some benefit should occurr&lt;br&gt;
thanks!&lt;br&gt;
&lt;br&gt;
P.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
"Matt J" wrote in message &amp;lt;ivtb1d$ng6$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Roger Stafford" wrote in message &amp;lt;ivt9ek$jq3$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;   What's wrong with just using a simple for-loop?  It may be about as efficient as any vectorization and is a lot easier to code.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; =========================&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I agree that this might be a case of vectorization overkill. Nevertheless, the following might be a better alternative to a for-loop if the strings of zeros are all fairly short&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; v_end=v_orig;&lt;br&gt;
&amp;gt; idx=1;&lt;br&gt;
&amp;gt; while any(idx)&lt;br&gt;
&amp;gt;    &lt;br&gt;
&amp;gt;    s = circshift(v_end,[0,-1]);&lt;br&gt;
&amp;gt;    idx=s &amp; ~v_end;&lt;br&gt;
&amp;gt;    v_end(idx)=s(idx);&lt;br&gt;
&amp;gt;    &lt;br&gt;
&amp;gt; end</description>
    </item>
    <item>
      <pubDate>Mon, 18 Jul 2011 06:23:08 +0000</pubDate>
      <title>Re: vectorization suggestion</title>
      <link>http://www.mathworks.es/matlabcentral/newsreader/view_thread/310547#845755</link>
      <author>Bruno Luong</author>
      <description>v_orig=[0 1 0 2 0 3 0 4 0 0 5 0 0 6]&lt;br&gt;
&amp;nbsp;&lt;br&gt;
v = fliplr(v_orig);&lt;br&gt;
idx = v~=0;&lt;br&gt;
d = diff(v(idx));&lt;br&gt;
idx(find(idx,1,'first')) = false;&lt;br&gt;
v(idx) = d;&lt;br&gt;
v = fliplr(cumsum(v))&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
  </channel>
</rss>
