<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239846</link>
    <title>MATLAB Central Newsreader - Vectorization of matrix operations--need guru's help!</title>
    <description>Feed for thread: Vectorization of matrix operations--need guru's help!</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>Mon, 24 Nov 2008 17:59:03 -0500</pubDate>
      <title>Vectorization of matrix operations--need guru's help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239846#612949</link>
      <author>Jerry </author>
      <description>is there any way to improve the speed of the following code? I need to call it 10000 times. thanks!&lt;br&gt;
&lt;br&gt;
function m4=calm4(mwk)&lt;br&gt;
[n0,n1]=size(mwk);&lt;br&gt;
m4=0;&lt;br&gt;
for i=1:n0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tmp1=mwk(:,i);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for ii=1:n0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ii~=i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tmp2=mwk(:,ii)*tmp1';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m4=m4+sum(sum(tmp2))-sum(diag(tmp2));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
m4=m4/(n0*n1*(n0-1)*(n1-1));</description>
    </item>
    <item>
      <pubDate>Mon, 24 Nov 2008 19:08:02 -0500</pubDate>
      <title>Re: Vectorization of matrix operations--need guru's help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239846#612972</link>
      <author>Roger Stafford</author>
      <description>&quot;Jerry&quot; &amp;lt;mricad@yahoo.no000spppam.com&amp;gt; wrote in message &amp;lt;ggeq17$hj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; is there any way to improve the speed of the following code? I need to call it 10000 times. thanks!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function m4=calm4(mwk)&lt;br&gt;
&amp;gt; [n0,n1]=size(mwk);&lt;br&gt;
&amp;gt; m4=0;&lt;br&gt;
&amp;gt; for i=1:n0&lt;br&gt;
&amp;gt;     tmp1=mwk(:,i);&lt;br&gt;
&amp;gt;     for ii=1:n0&lt;br&gt;
&amp;gt;         if ii~=i&lt;br&gt;
&amp;gt;             tmp2=mwk(:,ii)*tmp1';&lt;br&gt;
&amp;gt;             m4=m4+sum(sum(tmp2))-sum(diag(tmp2));&lt;br&gt;
&amp;gt;         end&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; m4=m4/(n0*n1*(n0-1)*(n1-1));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Jerry, notice that 'tmp2' consists of every possible product between elements of the i_th and ii_th columns of 'mwk'.  When you do sum(sum(tmp2)) you are taking the sum of all these possible products and by the distributive law of arithmetic that is the same as the product of the two column sums, which is a lot easier to compute.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Notice further that by similar reasoning, if we temporarily remove the restriction that ii~=i and take the sum of all possible values of these column-sum products above, this is the same as the product of the sum of all column-sums by itself, namely the square of the sum of all elements of 'mwk', which again would be an enormous simplification.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;In the ii==i cases the quantity 'sum(sum(tmp2))' is simply the square of the sum of the ii_th (i_th) column, and the sum of all these would be the sum of the squares of all the column sums, again a simply quantity to compute in compensating for their erroneous inclusion in the previous paragraph.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The quantity 'sum(diag(tmp2))' is simply the dot product of the ii_th column by the i_th column.  If we sum for every possible combination of ii and i, again ignoring the ii~=i constraint, this gives the dot product of the row-sums of 'mwk' by itself, which again is much easier to find.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Finally, for the ii==i cases 'sum(diag(tmp2))' is the dot product of the ii_th (i_th) column by itself.  The sum of these must then be subtracted to compensate for their erroneous inclusion above.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I leave it to you to implement these notions.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;There seems to be some confusion in your code as to whether n0 is the size of the row or the column of 'mwk'.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Mon, 24 Nov 2008 20:19:02 -0500</pubDate>
      <title>Re: Vectorization of matrix operations--need guru's help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239846#612996</link>
      <author>Roger Stafford</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;ggeu2i$1l4$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; ......&lt;br&gt;
&amp;gt;   I leave it to you to implement these notions.&lt;br&gt;
&amp;gt; ......&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Despite my intention to leave the implementation to you, curiosity got the better of me and I decided to see what the resulting code would look like.  Here it is.  See if you agree with it.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;m4 = (sum(sum(mwk,1))^2-sum(sum(mwk,1).^2)-sum(sum(mwk,2).^2) ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;+sum(sum(mwk.^2,1)))/(n0*n1*(n0-1)*(n1-1));&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Mon, 24 Nov 2008 22:34:02 -0500</pubDate>
      <title>Re: Vectorization of matrix operations--need guru's help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239846#613040</link>
      <author>Jerry </author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Despite my intention to leave the implementation to you, curiosity got the better of me and I decided to see what the resulting code would look like.  Here it is.  See if you agree with it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  m4 = (sum(sum(mwk,1))^2-sum(sum(mwk,1).^2)-sum(sum(mwk,2).^2) ...&lt;br&gt;
&amp;gt;       +sum(sum(mwk.^2,1)))/(n0*n1*(n0-1)*(n1-1));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
YES! And it's over 100 times faster! Thanks a million!&lt;br&gt;
I was trying to implement your ideas but made a mistake in the last term.</description>
    </item>
    <item>
      <pubDate>Mon, 24 Nov 2008 22:38:02 -0500</pubDate>
      <title>Re: Vectorization of matrix operations--need guru's help!</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239846#613042</link>
      <author>Jerry </author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &lt;br&gt;
&amp;gt;   There seems to be some confusion in your code as to whether n0 is the size of the row or the column of 'mwk'.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
You are right. should be&lt;br&gt;
[n1,n0]=size(mwk);</description>
    </item>
  </channel>
</rss>

