<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153283</link>
    <title>MATLAB Central Newsreader - How to vectorize and avoid this for loop to speed up?</title>
    <description>Feed for thread: How to vectorize and avoid this for loop to speed up?</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>Wed, 25 Jul 2007 00:07:41 -0400</pubDate>
      <title>How to vectorize and avoid this for loop to speed up?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153283#384713</link>
      <author>Linus Utopia</author>
      <description>Hi all,&lt;br&gt;
&lt;br&gt;
When inside a loop, there is a vector operator(r and p) already, how to &lt;br&gt;
further vectorize it along another dimension (for &quot;u&quot; and &quot;us&quot;)?&lt;br&gt;
&lt;br&gt;
Here is my code:&lt;br&gt;
&lt;br&gt;
---------------&lt;br&gt;
&lt;br&gt;
tmp=zeros(size(us));  %pre-allocation&lt;br&gt;
for i=1:length(us);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;u=us(i);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t7=u+b*p(:);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tmp(i)=exp(-u*a- r(:).'*t7(:) );&lt;br&gt;
end;&lt;br&gt;
&lt;br&gt;
---------------&lt;br&gt;
&lt;br&gt;
Let me explain. &quot;us&quot; is a vector. I generate &quot;tmp&quot; the same length as &quot;us&quot;.&lt;br&gt;
&lt;br&gt;
For each element of &quot;us&quot;, I compute an element for &quot;tmp&quot;.&lt;br&gt;
&lt;br&gt;
&quot;p&quot; and &quot;r&quot; are vectors of equal length.&lt;br&gt;
&lt;br&gt;
&quot;a&quot; and &quot;b&quot; are scalars and constants.&lt;br&gt;
&lt;br&gt;
r(:).'*t7(:)  is the dot-product of two vectors and hence it is a scalar.&lt;br&gt;
&lt;br&gt;
Hence each tmp(i) is a scalar.&lt;br&gt;
&lt;br&gt;
Is there a way to vectorize the above for loop?&lt;br&gt;
&lt;br&gt;
I have been pondering on this for long time but I am not that creative, or &lt;br&gt;
perhaps is lack of experience, etc. I couldn't come up with a good idea...&lt;br&gt;
&lt;br&gt;
Thanks! </description>
    </item>
    <item>
      <pubDate>Wed, 25 Jul 2007 07:38:01 -0400</pubDate>
      <title>Re: How to vectorize and avoid this for loop to speed up?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153283#384739</link>
      <author>Andrea Schmidt</author>
      <description>Hello,&lt;br&gt;
&lt;br&gt;
I don't know if it is faster, but it is without a for-loop:&lt;br&gt;
&lt;br&gt;
tmp=exp(-us*a-us*sum(r)-dot(r,b*p));  %dot is dot-product&lt;br&gt;
&lt;br&gt;
I hope that helps you.&lt;br&gt;
Best regards&lt;br&gt;
Andrea&lt;br&gt;
&lt;br&gt;
&quot;Linus Utopia&quot; &amp;lt;linus_utopia@gmail.com&amp;gt; schrieb im Newsbeitrag&lt;br&gt;
news:f86ief$f4n$1@news.Stanford.EDU...&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; When inside a loop, there is a vector operator(r and p) already, how to&lt;br&gt;
&amp;gt; further vectorize it along another dimension (for &quot;u&quot; and &quot;us&quot;)?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Here is my code:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ---------------&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tmp=zeros(size(us));  %pre-allocation&lt;br&gt;
&amp;gt; for i=1:length(us);&lt;br&gt;
&amp;gt;     u=us(i);&lt;br&gt;
&amp;gt;     t7=u+b*p(:);&lt;br&gt;
&amp;gt;     tmp(i)=exp(-u*a- r(:).'*t7(:) );&lt;br&gt;
&amp;gt; end;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ---------------&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Let me explain. &quot;us&quot; is a vector. I generate &quot;tmp&quot; the same length as&lt;br&gt;
&quot;us&quot;.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; For each element of &quot;us&quot;, I compute an element for &quot;tmp&quot;.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &quot;p&quot; and &quot;r&quot; are vectors of equal length.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &quot;a&quot; and &quot;b&quot; are scalars and constants.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; r(:).'*t7(:)  is the dot-product of two vectors and hence it is a scalar.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Hence each tmp(i) is a scalar.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Is there a way to vectorize the above for loop?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I have been pondering on this for long time but I am not that creative, or&lt;br&gt;
&amp;gt; perhaps is lack of experience, etc. I couldn't come up with a good idea...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;</description>
    </item>
    <item>
      <pubDate>Wed, 25 Jul 2007 22:28:44 -0400</pubDate>
      <title>Re: How to vectorize and avoid this for loop to speed up?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153283#384953</link>
      <author>Linus Utopia</author>
      <description>Hi Andrea,&lt;br&gt;
&lt;br&gt;
I am sorry but I forgot to add more details: Please see below. Here there &lt;br&gt;
was a &quot;log&quot; missing in the definition of t7. And c is a scalar constant...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tmp=zeros(size(us));  %pre-allocation&lt;br&gt;
&amp;gt;&amp;gt; for i=1:length(us);&lt;br&gt;
&amp;gt;&amp;gt;     u=us(i);&lt;br&gt;
&amp;gt;&amp;gt;     b=1/(u+c);&lt;br&gt;
&amp;gt;&amp;gt;     t7=log(u+b*p(:));&lt;br&gt;
&amp;gt;&amp;gt;     tmp(i)=exp(-u*a- r(:).'*t7(:) );&lt;br&gt;
&amp;gt;&amp;gt; end;&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; ---------------&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Let me explain. &quot;us&quot; is a vector. I generate &quot;tmp&quot; the same length as&lt;br&gt;
&amp;gt; &quot;us&quot;.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; For each element of &quot;us&quot;, I compute an element for &quot;tmp&quot;.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; &quot;p&quot; and &quot;r&quot; are vectors of equal length.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; &quot;a&quot; and &quot;b&quot; are scalars and constants.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; r(:).'*t7(:)  is the dot-product of two vectors and hence it is a scalar.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Hence each tmp(i) is a scalar.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Andrea Schmidt&quot; &amp;lt;external.Andrea.Schmidt@de.bosch.com&amp;gt; wrote in message &lt;br&gt;
news:f86njq$qf0$1@news4.fe.internet.bosch.com...&lt;br&gt;
&amp;gt; Hello,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I don't know if it is faster, but it is without a for-loop:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tmp=exp(-us*a-us*sum(r)-dot(r,b*p));  %dot is dot-product&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I hope that helps you.&lt;br&gt;
&amp;gt; Best regards&lt;br&gt;
&amp;gt; Andrea&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &quot;Linus Utopia&quot; &amp;lt;linus_utopia@gmail.com&amp;gt; schrieb im Newsbeitrag&lt;br&gt;
&amp;gt; news:f86ief$f4n$1@news.Stanford.EDU...&lt;br&gt;
&amp;gt;&amp;gt; Hi all,&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; When inside a loop, there is a vector operator(r and p) already, how to&lt;br&gt;
&amp;gt;&amp;gt; further vectorize it along another dimension (for &quot;u&quot; and &quot;us&quot;)?&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Here is my code:&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; ---------------&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; tmp=zeros(size(us));  %pre-allocation&lt;br&gt;
&amp;gt;&amp;gt; for i=1:length(us);&lt;br&gt;
&amp;gt;&amp;gt;     u=us(i);&lt;br&gt;
&amp;gt;&amp;gt;     t7=u+b*p(:);&lt;br&gt;
&amp;gt;&amp;gt;     tmp(i)=exp(-u*a- r(:).'*t7(:) );&lt;br&gt;
&amp;gt;&amp;gt; end;&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; ---------------&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Let me explain. &quot;us&quot; is a vector. I generate &quot;tmp&quot; the same length as&lt;br&gt;
&amp;gt; &quot;us&quot;.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; For each element of &quot;us&quot;, I compute an element for &quot;tmp&quot;.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; &quot;p&quot; and &quot;r&quot; are vectors of equal length.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; &quot;a&quot; and &quot;b&quot; are scalars and constants.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; r(:).'*t7(:)  is the dot-product of two vectors and hence it is a scalar.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Hence each tmp(i) is a scalar.&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Is there a way to vectorize the above for loop?&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; I have been pondering on this for long time but I am not that creative, &lt;br&gt;
&amp;gt;&amp;gt; or&lt;br&gt;
&amp;gt;&amp;gt; perhaps is lack of experience, etc. I couldn't come up with a good &lt;br&gt;
&amp;gt;&amp;gt; idea...&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Thanks!&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Thu, 26 Jul 2007 03:24:23 -0400</pubDate>
      <title>Re: How to vectorize and avoid this for loop to speed up?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153283#384961</link>
      <author>last) (first then</author>
      <description>What is c?</description>
    </item>
    <item>
      <pubDate>Thu, 26 Jul 2007 03:26:33 -0400</pubDate>
      <title>Re: How to vectorize and avoid this for loop to speed up?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153283#384962</link>
      <author>last) (first then</author>
      <description>Looks like you oughta do element by element matrix multiplication ... &lt;br&gt;
&lt;br&gt;
.* a couple of n x m matrices together to create another n x m matrix.</description>
    </item>
    <item>
      <pubDate>Thu, 26 Jul 2007 07:29:04 -0400</pubDate>
      <title>Re: How to vectorize and avoid this for loop to speed up?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153283#385008</link>
      <author>Linus Utopia</author>
      <description>&lt;br&gt;
&quot;last) (first then&quot; &amp;lt;nospam@nospamplease.com&amp;gt; wrote in message &lt;br&gt;
news:f89499$aul$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Looks like you oughta do element by element matrix multiplication ...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; .* a couple of n x m matrices together to create another n x m matrix.&lt;br&gt;
&lt;br&gt;
Sure I know how to do this. But how to do this efficiently.&lt;br&gt;
That's what I want to learn... If I don't keep learning creative things, I &lt;br&gt;
will be sticking to my naive implementation with no improvment. </description>
    </item>
  </channel>
</rss>

