<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416</link>
    <title>MATLAB Central Newsreader - averaging</title>
    <description>Feed for thread: averaging</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>Tue, 22 Sep 2009 21:06:03 -0400</pubDate>
      <title>averaging</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416#681933</link>
      <author>jenya polyakova</author>
      <description>I know it can be done in one line. But I am not sure how to do it. Anybody please?&lt;br&gt;
I have a matrix. Say,&lt;br&gt;
col1 col2&lt;br&gt;
23 0.6&lt;br&gt;
12 0.9&lt;br&gt;
12 1.0 &lt;br&gt;
23 0.5&lt;br&gt;
how to average col2 so that it averages by the same id number represented by col1 i.e it should give me the column of [(0.6+0.5)/2 ; (0.9+0.1)/2]. Thanks so much.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;</description>
    </item>
    <item>
      <pubDate>Wed, 23 Sep 2009 00:59:02 -0400</pubDate>
      <title>Re: averaging</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416#681960</link>
      <author>arich82 </author>
      <description>&quot;jenya polyakova&quot; &amp;lt;jenya56@yahoo.com&amp;gt; wrote in message &amp;lt;h9be7r$gh5$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I know it can be done in one line. But I am not sure how to do it. Anybody please?&lt;br&gt;
&amp;gt; I have a matrix. Say,&lt;br&gt;
&amp;gt; col1 col2&lt;br&gt;
&amp;gt; 23 0.6&lt;br&gt;
&amp;gt; 12 0.9&lt;br&gt;
&amp;gt; 12 1.0 &lt;br&gt;
&amp;gt; 23 0.5&lt;br&gt;
&amp;gt; how to average col2 so that it averages by the same id number represented by col1 i.e it should give me the column of [(0.6+0.5)/2 ; (0.9+1.0)/2]. Thanks so much.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  &lt;br&gt;
&lt;br&gt;
Assuming col1 is exactly integer so we can use it in boolean tests:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; mean([col2(col1==23), col2(col1==12)]) % assumes equal numbers of each id&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
A more robust method would use round(col1) for the boolean.  If you want some truly horrendous notation that keeps everything on one line and references only one matrix:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; A  = [23 0.6 ; 12 0.9 ; 12 1.0 ; 23 0.5]&lt;br&gt;
&amp;gt;&amp;gt; [mean(A(round(A(:, 1))==23, 2)), mean(A(round(A(:, 1))==12, 2))] &lt;br&gt;
% mean called twice &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Good luck parsing that.  It would be wiser to explicitly pull out the indices instead:&lt;br&gt;
&amp;gt;&amp;gt; id1 = round(23);&lt;br&gt;
&amp;gt;&amp;gt; id2 = round(12);&lt;br&gt;
&amp;gt;&amp;gt; col1 = round(A(:, 1));&lt;br&gt;
&amp;gt;&amp;gt; col2 = A(:, 2);&lt;br&gt;
&amp;gt;&amp;gt; k1 = col1 == id1;&lt;br&gt;
&amp;gt;&amp;gt; k2 = col1 == id2;&lt;br&gt;
&amp;gt;&amp;gt; [mean(col2(k1)), mean(col2(k2))]&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Hope this helps.&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Wed, 23 Sep 2009 06:17:03 -0400</pubDate>
      <title>Re: averaging</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416#681997</link>
      <author>jenya polyakova</author>
      <description>&quot;arich82 &quot; &amp;lt;|a|r|i|c|8|2|@hotmail.com&amp;gt; wrote in message &amp;lt;h9brsm$r8s$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;jenya polyakova&quot; &amp;lt;jenya56@yahoo.com&amp;gt; wrote in message &amp;lt;h9be7r$gh5$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I know it can be done in one line. But I am not sure how to do it. Anybody please?&lt;br&gt;
&amp;gt; &amp;gt; I have a matrix. Say,&lt;br&gt;
&amp;gt; &amp;gt; col1 col2&lt;br&gt;
&amp;gt; &amp;gt; 23 0.6&lt;br&gt;
&amp;gt; &amp;gt; 12 0.9&lt;br&gt;
&amp;gt; &amp;gt; 12 1.0 &lt;br&gt;
&amp;gt; &amp;gt; 23 0.5&lt;br&gt;
&amp;gt; &amp;gt; how to average col2 so that it averages by the same id number represented by col1 i.e it should give me the column of [(0.6+0.5)/2 ; (0.9+1.0)/2]. Thanks so much.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Assuming col1 is exactly integer so we can use it in boolean tests:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; mean([col2(col1==23), col2(col1==12)]) % assumes equal numbers of each id&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A more robust method would use round(col1) for the boolean.  If you want some truly horrendous notation that keeps everything on one line and references only one matrix:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; A  = [23 0.6 ; 12 0.9 ; 12 1.0 ; 23 0.5]&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; [mean(A(round(A(:, 1))==23, 2)), mean(A(round(A(:, 1))==12, 2))] &lt;br&gt;
&amp;gt; % mean called twice &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Good luck parsing that.  It would be wiser to explicitly pull out the indices instead:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; id1 = round(23);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; id2 = round(12);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; col1 = round(A(:, 1));&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; col2 = A(:, 2);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; k1 = col1 == id1;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; k2 = col1 == id2;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; [mean(col2(k1)), mean(col2(k2))]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope this helps.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; --&lt;br&gt;
&lt;br&gt;
but I do not know that id1=round(23). The matrix A was used just as example. My matrix A is HUGE. What I need is to sort by col 1 and then average col2 according to col1. Any suggestions?</description>
    </item>
    <item>
      <pubDate>Wed, 23 Sep 2009 06:48:01 -0400</pubDate>
      <title>Re: averaging</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416#682000</link>
      <author>Bruno Luong</author>
      <description>Use similar technique from this thread. Call UNIQUE on the first column if needed:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117&quot;&gt;http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Wed, 23 Sep 2009 07:44:05 -0400</pubDate>
      <title>Re: averaging</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416#682011</link>
      <author>arich82 </author>
      <description>&amp;gt; but I do not know that id1=round(23). The matrix A was used just as example. My matrix A is HUGE. What I need is to sort by col 1 and then average col2 according to col1. Any suggestions?&lt;br&gt;
&lt;br&gt;
Does this work?&lt;br&gt;
&lt;br&gt;
[dummy, dummy, i1] = unique(A(:,1));&lt;br&gt;
avg = accumarray(i1, A(:,2), [], @mean);&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Wed, 23 Sep 2009 07:47:04 -0400</pubDate>
      <title>Re: averaging</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416#682012</link>
      <author>arich82 </author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h9cgb0$g2s$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Use similar technique from this thread. Call UNIQUE on the first column if needed:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;a href=&quot;http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117&quot;&gt;http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117&lt;/a&gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
... And had I refreshed my browser before posting, I would have seen that Bruno already solved your problem.&lt;br&gt;
&lt;br&gt;
Sorry for the duplicating the post (but at least I learned something in the process!).&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Wed, 23 Sep 2009 11:08:04 -0400</pubDate>
      <title>Re: averaging</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261416#682043</link>
      <author>Jos </author>
      <description>&quot;arich82 &quot; &amp;lt;|a|r|i|c|8|2|@hotmail.com&amp;gt; wrote in message &amp;lt;h9cjpo$sma$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h9cgb0$g2s$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Use similar technique from this thread. Call UNIQUE on the first column if needed:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;a href=&quot;http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117&quot;&gt;http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117&lt;/a&gt;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Bruno&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ... And had I refreshed my browser before posting, I would have seen that Bruno already solved your problem.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sorry for the duplicating the post (but at least I learned something in the process!).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; --&lt;br&gt;
&lt;br&gt;
Here is another &quot;oneliner&quot; using cells&lt;br&gt;
&lt;br&gt;
% data&lt;br&gt;
A = [23 0.6 ; 12 0.9 ; 12 1.0 ; 23 0.5] ;&lt;br&gt;
% engine&lt;br&gt;
R = cellfun(@mean,group2cell(A(:,2),A(:,1)))&lt;br&gt;
&lt;br&gt;
My GROUP2CELL submission can be found here:&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/11192&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/11192&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
Jos</description>
    </item>
  </channel>
</rss>

