<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300</link>
    <title>MATLAB Central Newsreader - Give me a good example for accumarray()</title>
    <description>Feed for thread: Give me a good example for accumarray()</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>Sat, 04 Jul 2009 08:54:01 -0400</pubDate>
      <title>Give me a good example for accumarray()</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300#662581</link>
      <author>Husam Aldahiyat</author>
      <description>Hello,&lt;br&gt;
I am learning MATLAB and when I read about the accumarray() function it looks good but I didn't understand how the examples worked. &lt;br&gt;
&lt;br&gt;
Could someone give me a nice small example (problem) and how accumarray solves it?</description>
    </item>
    <item>
      <pubDate>Sat, 04 Jul 2009 09:55:02 -0400</pubDate>
      <title>Re: Give me a good example for accumarray()</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300#662583</link>
      <author>John D'Errico</author>
      <description>&quot;Husam Aldahiyat&quot; &amp;lt;numandina@gmail.com&amp;gt; wrote in message &amp;lt;h2n5b9$lo7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello,&lt;br&gt;
&amp;gt; I am learning MATLAB and when I read about the accumarray() function it looks good but I didn't understand how the examples worked. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Could someone give me a nice small example (problem) and how accumarray solves it?&lt;br&gt;
&lt;br&gt;
Any time that you wish to count the number of times&lt;br&gt;
an element appears in a vector, accumarray is the&lt;br&gt;
solution. Best is if the elements are small positive&lt;br&gt;
integers.&lt;br&gt;
&lt;br&gt;
x = ceil(10*rand(1,20))&lt;br&gt;
x =&lt;br&gt;
&amp;nbsp;7  1  9  10  7  8  8  4  7  2  8  1  3  1  1  9  7  4  10  1&lt;br&gt;
&lt;br&gt;
count = accumarray(x',1)&lt;br&gt;
count =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;br&gt;
&lt;br&gt;
If they are not small positive integers, then the third&lt;br&gt;
argument of unique will provide the extra piece.&lt;br&gt;
&lt;br&gt;
If you wish to see accumarray in action, it forms an&lt;br&gt;
important part of my own consolidator utility on the&lt;br&gt;
file exchange:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/8354&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/8354&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
Of course, accumarray has other uses, since it can do&lt;br&gt;
more than just count a list of numbers. But, this is&lt;br&gt;
the task I set to accumarray more often than any other.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sat, 04 Jul 2009 15:26:01 -0400</pubDate>
      <title>Re: Give me a good example for accumarray()</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300#662605</link>
      <author>Matt Fig</author>
      <description>Here's an odd one.  &lt;br&gt;
&lt;br&gt;
Say you wanted to find the row number in which each unique value in a matrix of positive integers appears, then store these row numbers in a cell array.  (Don't ask.)&lt;br&gt;
&lt;br&gt;
M = ceil(rand(20,3)*10); % Data.&lt;br&gt;
C = accumarray(M(:), repmat((1:size(M,1))', [size(M,2) 1]), [], @(x) {x});</description>
    </item>
    <item>
      <pubDate>Sat, 04 Jul 2009 18:32:00 -0400</pubDate>
      <title>Re: Give me a good example for accumarray()</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300#662620</link>
      <author>Husam Aldahiyat</author>
      <description>Thanks for the responses. I'm trying to learn of it and of arrayfun (I'm good with cellfun but having troubles with arrayfun). I want to do this for example:&lt;br&gt;
&lt;br&gt;
say I have a matrix s;&lt;br&gt;
&lt;br&gt;
s =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;6     1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5     2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;8     4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3     7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;7     7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3     8&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;8     8&lt;br&gt;
&lt;br&gt;
I want to find the rows which have a similar first column and a second column that is different by +/- 1. &lt;br&gt;
&lt;br&gt;
For example for first column value (1) I will get&lt;br&gt;
&lt;br&gt;
1 1&lt;br&gt;
1 2&lt;br&gt;
1 3&lt;br&gt;
&lt;br&gt;
and for first column value (3)&lt;br&gt;
&lt;br&gt;
3 7&lt;br&gt;
3 8&lt;br&gt;
&lt;br&gt;
I can do a loop but my one liner attempts have failed. I'm trying this but it's wrong. Can someone please tell me what I'm doing wrong and if there is a way using accumarray or arrayfun? Thanks!!!&lt;br&gt;
&lt;br&gt;
% for first column value 1 I want second column values that are close&lt;br&gt;
s(arrayfun(@(x,y)x==1 &amp;&amp; sum(ismember(y(x==1),y-1:y+1)),s(:,1),s(:,2)),2)&lt;br&gt;
&lt;br&gt;
Help!</description>
    </item>
    <item>
      <pubDate>Sat, 04 Jul 2009 18:57:01 -0400</pubDate>
      <title>Re: Give me a good example for accumarray()</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300#662624</link>
      <author>Matt Fig</author>
      <description>&quot;Husam Aldahiyat&quot; &amp;lt;numandina@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; say I have a matrix s;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      1     1&lt;br&gt;
&amp;gt;      6     1&lt;br&gt;
&amp;gt;      1     2&lt;br&gt;
&amp;gt;      5     2&lt;br&gt;
&amp;gt;      1     3&lt;br&gt;
&amp;gt;      2     3&lt;br&gt;
&amp;gt;      8     4&lt;br&gt;
&amp;gt;      1     7&lt;br&gt;
&amp;gt;      3     7&lt;br&gt;
&amp;gt;      7     7&lt;br&gt;
&amp;gt;      3     8&lt;br&gt;
&amp;gt;      8     8&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want to find the rows which have a similar first column and a second column that is different by +/- 1. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For example for first column value (1) I will get&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1 1&lt;br&gt;
&amp;gt; 1 2&lt;br&gt;
&amp;gt; 1 3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and for first column value (3)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 3 7&lt;br&gt;
&amp;gt; 3 8&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
How is (7,8) within +/-1 of 3?  If you mean to find the values in the second column which are +/-1 from each other, what do you expect when there are two such sets?  Are we to return both sets?  For example:&lt;br&gt;
&lt;br&gt;
s = &lt;br&gt;
3 7&lt;br&gt;
3 8&lt;br&gt;
2 9&lt;br&gt;
1 0&lt;br&gt;
3 60&lt;br&gt;
3 61&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Maybe posting your for loop would clarify this matter.</description>
    </item>
    <item>
      <pubDate>Mon, 06 Jul 2009 15:55:40 -0400</pubDate>
      <title>Re: Give me a good example for accumarray()</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300#662958</link>
      <author>Peter Perkins</author>
      <description>Husam Aldahiyat wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; For example for first column value (1) I will get&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1 1&lt;br&gt;
&amp;gt; 1 2&lt;br&gt;
&amp;gt; 1 3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and for first column value (3)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 3 7&lt;br&gt;
&amp;gt; 3 8&lt;br&gt;
&lt;br&gt;
ACCUMARRAY may or may not be able to help here.  The SUBS input determines how values in the VALS input are batched up when passed to your FUN, but FUN has no access to the SUBS values themselves.  Thus, AUUMARRAY can easily return [1; 2; 3], but you will need to do something additional to get [1 1; 1 2; 1 3].</description>
    </item>
    <item>
      <pubDate>Sun, 05 Sep 2010 07:36:06 -0400</pubDate>
      <title>Re: Give me a good example for accumarray()</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255300#777134</link>
      <author>ole asdf</author>
      <description>Ok, quite old thread, nevertheless I decided to post, because I'm quite happy right now, because I found out, accumarray() solves my problem perfectly:&lt;br&gt;
&lt;br&gt;
Given you have a statistic &quot;stat&quot; in which results from different games are stored (one game per row). In the first column is a player id, in the second column a gain/loss for the player with a specific player id. The matrix &quot;stat&quot; contains infos about many games and one player can have multiple entries (one for every game he played). If you want to get the cumulated gain/loss for every player, you calculate: cumulated_score = accumarray(stat(:,1) , stat(:,2)) which returns a column vector. (Players with an id that is not contained in stat(:,1) will get a score of 0.) To get the cumulated score of player with id = 5 you enter cumulated_score(5).&lt;br&gt;
&lt;br&gt;
Implementing this (in a fast manner) by yourself can be quite a PITA...</description>
    </item>
  </channel>
</rss>

