<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238185</link>
    <title>MATLAB Central Newsreader - computing sum of squared deviations without a for-loop</title>
    <description>Feed for thread: computing sum of squared deviations without a for-loop</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>Sun, 26 Oct 2008 14:08:37 -0400</pubDate>
      <title>computing sum of squared deviations without a for-loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238185#607379</link>
      <author>Lee</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
I need to compute a matrix, S.&lt;br&gt;
S is a m by m matrix.  It is representing sum of squared deviations.&lt;br&gt;
&lt;br&gt;
Currently, it is computed using a for-loop:&lt;br&gt;
&lt;br&gt;
S=[];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for i=1:N&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x=G(:,i)-g_bar;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;S=S+(x*x');&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
where the inputs are:&lt;br&gt;
G is a m by N matrix.&lt;br&gt;
G(:,i) is the ith column of G,&lt;br&gt;
g_bar is a m by 1 vector.&lt;br&gt;
&lt;br&gt;
If you write out this for-loop mathematically, you can see S is simply&lt;br&gt;
the sum of squared deviations.  But is there a way to compute it from&lt;br&gt;
the inputs without such a for-loop?</description>
    </item>
    <item>
      <pubDate>Sun, 26 Oct 2008 19:42:01 -0400</pubDate>
      <title>Re: computing sum of squared deviations without a for-loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238185#607405</link>
      <author>Roger Stafford</author>
      <description>Lee &amp;lt;kaloklee@gmail.com&amp;gt; wrote in message &amp;lt;be6e1c36-aa98-427b-b1d6-8ac03cc0f4ef@d45g2000hsc.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need to compute a matrix, S.&lt;br&gt;
&amp;gt; S is a m by m matrix.  It is representing sum of squared deviations.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Currently, it is computed using a for-loop:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; S=[];&lt;br&gt;
&amp;gt;       for i=1:N&lt;br&gt;
&amp;gt;           x=G(:,i)-g_bar;&lt;br&gt;
&amp;gt;           S=S+(x*x');&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; where the inputs are:&lt;br&gt;
&amp;gt; G is a m by N matrix.&lt;br&gt;
&amp;gt; G(:,i) is the ith column of G,&lt;br&gt;
&amp;gt; g_bar is a m by 1 vector.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you write out this for-loop mathematically, you can see S is simply&lt;br&gt;
&amp;gt; the sum of squared deviations.  But is there a way to compute it from&lt;br&gt;
&amp;gt; the inputs without such a for-loop?&lt;br&gt;
------------&lt;br&gt;
&amp;nbsp;&amp;nbsp;Yesterday I answered this same question in your previous thread with:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;G2 = G-repmat(g_bar,1,N);&lt;br&gt;
&amp;nbsp;S = G2*G2';&lt;br&gt;
&lt;br&gt;
Was there something objectionable with that answer?  It gives the same answer as in your code (aside from the expected round-off errors) and avoids the for-loop construct.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Wed, 29 Oct 2008 13:32:23 -0400</pubDate>
      <title>Re: computing sum of squared deviations without a for-loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238185#607900</link>
      <author>dpb</author>
      <description>Lee wrote:&lt;br&gt;
...&lt;br&gt;
&amp;gt; S=[];&lt;br&gt;
&amp;gt;       for i=1:N&lt;br&gt;
&amp;gt;           x=G(:,i)-g_bar;&lt;br&gt;
&amp;gt;           S=S+(x*x');&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; where the inputs are:&lt;br&gt;
&amp;gt; G is a m by N matrix.&lt;br&gt;
&amp;gt; G(:,i) is the ith column of G,&lt;br&gt;
&amp;gt; g_bar is a m by 1 vector.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you write out this for-loop mathematically, you can see S is simply&lt;br&gt;
&amp;gt; the sum of squared deviations.  But is there a way to compute it from&lt;br&gt;
&amp;gt; the inputs without such a for-loop?&lt;br&gt;
&lt;br&gt;
doc std&lt;br&gt;
&lt;br&gt;
Specifically, look at the optional arguments--don't they allow the &lt;br&gt;
requested?&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
  </channel>
</rss>

