<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240093</link>
    <title>MATLAB Central Newsreader - problem vectorizing</title>
    <description>Feed for thread: problem vectorizing</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>Fri, 28 Nov 2008 23:30:23 -0500</pubDate>
      <title>problem vectorizing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240093#613795</link>
      <author>Zsolt </author>
      <description>I would like to vectorize the double for loop below, can't find matlab matrix operator to proceed, can anyone help?  Note, current implementation performs the correct operation, it just takes way to long for data size I need to calculate for&lt;br&gt;
&lt;br&gt;
X = linspace(-a/2,a/2,length(Gpp));&lt;br&gt;
[X Y] = meshgrid(X);&lt;br&gt;
r = sqrt((X).^2+(Y).^2);&lt;br&gt;
tic&lt;br&gt;
ep = zeros(size(Egp));&lt;br&gt;
&lt;br&gt;
for m=1:length(Gpp)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for n=1:length(Gpp)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Sat, 29 Nov 2008 01:59:02 -0500</pubDate>
      <title>Re: problem vectorizing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240093#613801</link>
      <author>Jan Simon</author>
      <description>Dear Zsolt!&lt;br&gt;
&lt;br&gt;
&amp;gt; X = linspace(-a/2,a/2,length(Gpp));&lt;br&gt;
&amp;gt; [X Y] = meshgrid(X);&lt;br&gt;
&amp;gt; r = sqrt((X).^2+(Y).^2);&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; ep = zeros(size(Egp));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for m=1:length(Gpp)&lt;br&gt;
&amp;gt;     for n=1:length(Gpp)&lt;br&gt;
&amp;gt;         ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
Can you give us a definition of Gpp and Egp?&lt;br&gt;
Is LENGTH(Gpp) save enough - I'd prefer SIZE(Gpp,1).&lt;br&gt;
&lt;br&gt;
Kind regards, Jan</description>
    </item>
    <item>
      <pubDate>Sat, 29 Nov 2008 02:16:01 -0500</pubDate>
      <title>Re: problem vectorizing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240093#613803</link>
      <author>Roger Stafford</author>
      <description>&quot;Zsolt &quot; &amp;lt;zlpst@yahoo.com&amp;gt; wrote in message &amp;lt;ggpuuf$scq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I would like to vectorize the double for loop below, can't find matlab matrix operator to proceed, can anyone help?  Note, current implementation performs the correct operation, it just takes way to long for data size I need to calculate for&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; X = linspace(-a/2,a/2,length(Gpp));&lt;br&gt;
&amp;gt; [X Y] = meshgrid(X);&lt;br&gt;
&amp;gt; r = sqrt((X).^2+(Y).^2);&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; ep = zeros(size(Egp));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for m=1:length(Gpp)&lt;br&gt;
&amp;gt;     for n=1:length(Gpp)&lt;br&gt;
&amp;gt;         ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I presume Gpp, Egp, r, and ep are all square matrices of the same size.  You might conceivably gain a little time by changing the order here and completing the summation within each loop as in:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;ep = zeros(size(Egp));&lt;br&gt;
&amp;nbsp;for m=1:length(Gpp)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;for n=1:length(Gpp)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ep(m,n) = sum(sum(Egp.*exp(i*Gpp*r(m,n))));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
(or then again this might not help.)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;However, I don't see the need for any further vectorization.  If, as you indicate, these matrices are of large size, the overhead involved in the for-loop indexing should be quite small compared with the time used in the summation, multiplication, and exponentiation computations within each loop period.  I remind you that, as you have written the code, you are dealing here with every possible combination of elements from the matrix r with those of Gpp and Egp, which makes it an order(length(Gpp)^4) procedure and that is bound to take a lot of time for large length(Gpp).  Each doubling of length(Gpp) will multiply the time of execution by a factor of approximately sixteen!  Vectorization is not going to get you around that.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 29 Nov 2008 20:01:02 -0500</pubDate>
      <title>Re: problem vectorizing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/240093#613894</link>
      <author>Zsolt </author>
      <description>&quot;Zsolt &quot; &amp;lt;zlpst@yahoo.com&amp;gt; wrote in message &amp;lt;ggpuuf$scq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I would like to vectorize the double for loop below, can't find matlab matrix operator to proceed, can anyone help?  Note, current implementation performs the correct operation, it just takes way to long for data size I need to calculate for&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; X = linspace(-a/2,a/2,length(Gpp));&lt;br&gt;
&amp;gt; [X Y] = meshgrid(X);&lt;br&gt;
&amp;gt; r = sqrt((X).^2+(Y).^2);&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; ep = zeros(size(Egp));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for m=1:length(Gpp)&lt;br&gt;
&amp;gt;     for n=1:length(Gpp)&lt;br&gt;
&amp;gt;         ep = ep + Egp(m,n)*exp(i*Gpp(m,n).*r);&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
Thank you guys for the responses, Gpp and Egp are square 2D matrices of hundreds of thousands of ellements.  That is correct, Every value of Gpp is smeared across the r matrix and the same with Egp.</description>
    </item>
  </channel>
</rss>

