<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343</link>
    <title>MATLAB Central Newsreader - Re: HOWTO: Accelerate processing algorithm</title>
    <description>Feed for thread: Re: HOWTO: Accelerate processing algorithm</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, 05 Jul 2009 14:06:21 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662709</link>
      <author>Jose Antonio</author>
      <description>Ok, I think this makes it&lt;br&gt;
&lt;br&gt;
S = accumarray(y(:)+1,v(:), [], @mean)</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 14:25:02 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662710</link>
      <author>Bruno Luong</author>
      <description>Jose Antonio &amp;lt;juriguen@gmail.com&amp;gt; wrote in message &amp;lt;14646834.75779.1246802812202.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; Ok, I think this makes it&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; S = accumarray(y(:)+1,v(:), [], @mean)&lt;br&gt;
&lt;br&gt;
Jose,&lt;br&gt;
&amp;nbsp;&lt;br&gt;
I believe the above might be not the fastest, this could be even faster:&lt;br&gt;
&lt;br&gt;
yp1 = y(:)+1;&lt;br&gt;
S = accumarray(yp1,v(:))./accumarray(yp1,1);&lt;br&gt;
&lt;br&gt;
accumarray likes to work with 'sum'. Just a though&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 14:55:47 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662711</link>
      <author>Jose Antonio</author>
      <description>Very good point Bruno&lt;br&gt;
&lt;br&gt;
It does actually perform much faster. Using @mean I increased from 2s to 4s, and with your new advice, again back to 2.5s&lt;br&gt;
&lt;br&gt;
Thans!</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 15:48:19 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662714</link>
      <author>Rune Allnor</author>
      <description>On 5 Jul, 16:25, &quot;Bruno Luong&quot; &amp;lt;b.lu...@fogale.findmycountry&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; yp1 = y(:)+1;&lt;br&gt;
&amp;gt; S = accumarray(yp1,v(:))./accumarray(yp1,1);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; accumarray likes to work with 'sum'. Just a though&lt;br&gt;
&lt;br&gt;
I tried to figure out what ACCUMARRAY does, but let's&lt;br&gt;
say the documentation leaves more questions than it&lt;br&gt;
answers.&lt;br&gt;
&lt;br&gt;
Exactly what does the lines above do? In as plain English&lt;br&gt;
as possible, please.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 16:04:02 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662720</link>
      <author>Bruno Luong</author>
      <description>Rune,&lt;br&gt;
&lt;br&gt;
In short, ACCUMARRAY does for nd-array what SPARSE does for sparse matrix. It accumulates the vector elements (second input argument) depending on where they are located on a 'cell' (defined by the first input argument of subindexes).&lt;br&gt;
&lt;br&gt;
The subindex/vector might come in random order and arbitrary length. ACCUMARRAY just add them together on each cell. The sum result is then retuned as the array, each element corresponds to a 'cell'.&lt;br&gt;
&lt;br&gt;
The &quot;adding&quot; behavior can be changed by passing a use function handle (such as @mean).&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 16:17:45 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662722</link>
      <author>Jose Antonio</author>
      <description>Hi!&lt;br&gt;
&lt;br&gt;
This&lt;br&gt;
S = accumarray(yp1, v(:))./accumarray(yp1,1);&lt;br&gt;
&lt;br&gt;
Is equivalent to&lt;br&gt;
S = accumarray(yp1, v(:), [], @mean)&lt;br&gt;
&lt;br&gt;
Regarding the documentation, I have to admit it is also quite weird to me.&lt;br&gt;
&lt;br&gt;
What I wanted to do was to obtain an array, S, whose indices are specified by yp1, formed as the average of the values in v which are equal to the indices.&lt;br&gt;
&lt;br&gt;
For that purpose&lt;br&gt;
accumarray([1 1 3 2 2 5].', [1 2 3 4 5 6].')&lt;br&gt;
1 -&amp;gt; 1 + 2&lt;br&gt;
2 -&amp;gt; 4 + 5&lt;br&gt;
3 -&amp;gt; 3&lt;br&gt;
4&lt;br&gt;
5 -&amp;gt; 6&lt;br&gt;
&lt;br&gt;
And if you specify @fun, it performs any operation with the grouped elements&lt;br&gt;
accumarray([1 1 3 2 2 5].', [1 2 3 4 5 6].', [], @fun)&lt;br&gt;
1 -&amp;gt; fun(1,2)&lt;br&gt;
2 -&amp;gt; fun(4,5)&lt;br&gt;
3 -&amp;gt; fun(3)&lt;br&gt;
4&lt;br&gt;
5 -&amp;gt; fun(6)&lt;br&gt;
&lt;br&gt;
The final suggestion by Bruno basically performs the mean of the groups, because it gathers on one hand the elements of v and on the other all 1s, and sums the contents.&lt;br&gt;
accumarray([1 1 3 2 2 5].', [1 2 3 4 5 6].')&lt;br&gt;
accumarray([1 1 3 2 2 5].', [1 1 1 1 1 1].')&lt;br&gt;
1 -&amp;gt; 1 + 2           1 + 1&lt;br&gt;
2 -&amp;gt; 4 + 5           1 + 1&lt;br&gt;
3 -&amp;gt; 3               1&lt;br&gt;
4                    0&lt;br&gt;
5 -&amp;gt; 6               1&lt;br&gt;
&lt;br&gt;
It has worked fine for me, but explaining it to you now I have just realized that there might be a problem with 0/0.&lt;br&gt;
&lt;br&gt;
Hope it helps!&lt;br&gt;
Jose</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 16:28:01 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662723</link>
      <author>Bruno Luong</author>
      <description>Here is a code to show what ACCUMARRAY does:&lt;br&gt;
&lt;br&gt;
% Generate test data&lt;br&gt;
m = 10;&lt;br&gt;
nd = 3;&lt;br&gt;
sidx=ceil(4*rand(m,3)); % must be integers&lt;br&gt;
v=rand(m,1);&lt;br&gt;
&lt;br&gt;
% ACCUMARRAY DOES THIS&lt;br&gt;
sz = max(sidx,[],1);&lt;br&gt;
res = zeros(sz);&lt;br&gt;
for k=1:m&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sk = sidx(k,:);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sk = num2cell(sk);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;res(sk{:}) = res(sk{:}) + v(k,1);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% Check&lt;br&gt;
isequal(accumarray(sidx, v), res) % OK 1&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 16:28:15 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662724</link>
      <author>Jose Antonio</author>
      <description>Ok, easy enough&lt;br&gt;
&lt;br&gt;
S(isnan(S)) = 0&lt;br&gt;
&lt;br&gt;
solves the problem, and allows to get the same result returned by @mean, but much faster!&lt;br&gt;
&lt;br&gt;
Jose</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 16:33:01 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662725</link>
      <author>Bruno Luong</author>
      <description>&amp;nbsp;&amp;gt; I have just realized that there might be a problem with 0/0.&lt;br&gt;
&lt;br&gt;
You can specify the default value; if you prefer the accumarray-mean to return 0 for empty set; then call&lt;br&gt;
&lt;br&gt;
S = accumarray(yp1,v(:))./accumarray(yp1,1,[],[],1);&lt;br&gt;
&lt;br&gt;
Personally I prefer NaN for such case.&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 16:57:20 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662726</link>
      <author>Jose Antonio</author>
      <description>Thanks again!&lt;br&gt;
&lt;br&gt;
It always appears to be an easier / faster way to do things than I find out! :) Jaja</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 18:15:41 -0400</pubDate>
      <title>Re: HOWTO: Accelerate processing algorithm</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255343#662730</link>
      <author>Rune Allnor</author>
      <description>On 5 Jul, 18:04, &quot;Bruno Luong&quot; &amp;lt;b.lu...@fogale.findmycountry&amp;gt; wrote:&lt;br&gt;
&amp;gt; Rune,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; In short, ACCUMARRAY does for nd-array what SPARSE does for sparse matrix. It accumulates the vector elements (second input argument) depending on where they are located on a 'cell' (defined by the first input argument of subindexes).&lt;br&gt;
&lt;br&gt;
So the OP's code essentially builds a histogram&lt;br&gt;
from the images? If so, I tested a couple of variants&lt;br&gt;
of building histograms some time ago:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://groups.google.no/group/comp.soft-sys.matlab/msg/0b6492cb37b5d7fd?hl=no&quot;&gt;http://groups.google.no/group/comp.soft-sys.matlab/msg/0b6492cb37b5d7fd?hl=no&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
If you do things 'properly', you can easily speed things&lt;br&gt;
up a factor &amp;gt;10, using C++ instead of ACCUMARRAY.&lt;br&gt;
&lt;br&gt;
If this is a 'naive' grey-scale histogram, things might&lt;br&gt;
be simplified a lot more, with maybe another factor 5-10&lt;br&gt;
faster.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
  </channel>
</rss>

