<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Assignment with Repeated Indices</title>
	<atom:link href="http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Ravi</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16359</link>
		<dc:creator>Ravi</dc:creator>
		<pubDate>Wed, 08 Aug 2007 09:04:06 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16359</guid>
		<description>Steve,
Thanks a lot. Your tip put me on track. I now understand that a simple extension of logic is all that is required to proceed from a vector argument to a matrix one. I just did not get this before.</description>
		<content:encoded><![CDATA[<p>Steve,<br />
Thanks a lot. Your tip put me on track. I now understand that a simple extension of logic is all that is required to proceed from a vector argument to a matrix one. I just did not get this before.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve L</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16358</link>
		<dc:creator>Steve L</dc:creator>
		<pubDate>Wed, 08 Aug 2007 02:04:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16358</guid>
		<description>Ravi,

The first column of the subs matrix is the row indices of the elements you want to create in the result matrices.  The second column is the column indices.  The third column is the page indices (dimension 3.)  In general, the nth column of the subs input contains the indices in the nth dimension of the result matrix.

For instance, if one of the rows of the SUBS matrix is [1 2 3 4 5], then the entry in the corresponding row of VAL will be &quot;accumulated&quot; into element A(1, 2, 3, 4, 5) of the output matrix A of your call to ACCUMARRAY.  Try:

A = accumarray([1 2 3;4 5 6], [1;2]);
A(1, 2, 3)
A(4, 5, 6)</description>
		<content:encoded><![CDATA[<p>Ravi,</p>
<p>The first column of the subs matrix is the row indices of the elements you want to create in the result matrices.  The second column is the column indices.  The third column is the page indices (dimension 3.)  In general, the nth column of the subs input contains the indices in the nth dimension of the result matrix.</p>
<p>For instance, if one of the rows of the SUBS matrix is [1 2 3 4 5], then the entry in the corresponding row of VAL will be &#8220;accumulated&#8221; into element A(1, 2, 3, 4, 5) of the output matrix A of your call to ACCUMARRAY.  Try:</p>
<p>A = accumarray([1 2 3;4 5 6], [1;2]);<br />
A(1, 2, 3)<br />
A(4, 5, 6)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16354</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 07 Aug 2007 10:39:36 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16354</guid>
		<description>Ravi-

Sorry, I don&#039;t know of any others.  I recommend you pull apart the example(s) that you find confusing into smaller pieces and make sure you understand each piece.  :ooking at each piece separately should be helpful.

--Loren</description>
		<content:encoded><![CDATA[<p>Ravi-</p>
<p>Sorry, I don&#8217;t know of any others.  I recommend you pull apart the example(s) that you find confusing into smaller pieces and make sure you understand each piece.  :ooking at each piece separately should be helpful.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravi</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16353</link>
		<dc:creator>Ravi</dc:creator>
		<pubDate>Tue, 07 Aug 2007 09:34:31 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16353</guid>
		<description>I have a problem in understanding the accumarray function. In case the first argument is a vector, I have managed with the help section to follow what the function does. But when the first argument is a matrix (&quot;subs&quot; in the help documentation for the function), I am just unable to understand just what the function does. Loren, can you direct me to some documentation, apart from that in the help, that is more helpful for ordinary mortals like me.</description>
		<content:encoded><![CDATA[<p>I have a problem in understanding the accumarray function. In case the first argument is a vector, I have managed with the help section to follow what the function does. But when the first argument is a matrix (&#8220;subs&#8221; in the help documentation for the function), I am just unable to understand just what the function does. Loren, can you direct me to some documentation, apart from that in the help, that is more helpful for ordinary mortals like me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Davis</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16341</link>
		<dc:creator>Tim Davis</dc:creator>
		<pubDate>Thu, 02 Aug 2007 20:17:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16341</guid>
		<description>Loren replied on July 26th, 2007 at 11:29 am :
&gt; Mike, if you’re not using sparse in your problem otherwise
&gt; you will likely find that using full(sparse…)) is less
&gt; efficient than accumarray because of the burden of
&gt; creating and then transforming a sparse data structure.

In MATLAB 7.4, if you use the magic word

spparms(&#039;chmodsp&#039;,1)

and then try full(sparse( ...)), then you&#039;ll find that accumary and full(sparse) are about the same speed.  That&#039;s the case for the C=accumarray(...) above, even when iters is 100,000 for this example.</description>
		<content:encoded><![CDATA[<p>Loren replied on July 26th, 2007 at 11:29 am :<br />
&gt; Mike, if you’re not using sparse in your problem otherwise<br />
&gt; you will likely find that using full(sparse…)) is less<br />
&gt; efficient than accumarray because of the burden of<br />
&gt; creating and then transforming a sparse data structure.</p>
<p>In MATLAB 7.4, if you use the magic word</p>
<p>spparms(&#8216;chmodsp&#8217;,1)</p>
<p>and then try full(sparse( &#8230;)), then you&#8217;ll find that accumary and full(sparse) are about the same speed.  That&#8217;s the case for the C=accumarray(&#8230;) above, even when iters is 100,000 for this example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Davis</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16340</link>
		<dc:creator>Tim Davis</dc:creator>
		<pubDate>Thu, 02 Aug 2007 20:13:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16340</guid>
		<description>accumarray for Piotr&#039;s example is slower than sparse, only because each of the ith (i = 1:1000) submatrices are sparse.
accumarry and sparse are being used 1000 times each.

Faster than any of these methods, including the sub2ind method, is to avoid assignment with indices altogether, and do this:


    n = 1000 ; k = 50 ; iters = 1000 ;
    X = zeros (iters,k) ;
    Y = zeros (iters,k) ;
    for i=1:iters
        X (i,:) = randsample (n,k,true) ;
        Y (i,:) = randsample (n,k,true) ;
    end
    C = accumarray ([X(:) Y(:)], 1, [n n]) ;


On my machine, the for i=1:1000 code above using sparse inside the for loop takes 21.5 seconds, your sub2ind code takes 1.3 seconds, and the above code takes 0.2 seconds.  The above code also uses far less memory than your sub2ind code, too.

I find that it&#039;s often faster to avoid fancy indexing, and do as many whole-matrix operations as possible (such as the above accumarray).</description>
		<content:encoded><![CDATA[<p>accumarray for Piotr&#8217;s example is slower than sparse, only because each of the ith (i = 1:1000) submatrices are sparse.<br />
accumarry and sparse are being used 1000 times each.</p>
<p>Faster than any of these methods, including the sub2ind method, is to avoid assignment with indices altogether, and do this:</p>
<p>    n = 1000 ; k = 50 ; iters = 1000 ;<br />
    X = zeros (iters,k) ;<br />
    Y = zeros (iters,k) ;<br />
    for i=1:iters<br />
        X (i,:) = randsample (n,k,true) ;<br />
        Y (i,:) = randsample (n,k,true) ;<br />
    end<br />
    C = accumarray ([X(:) Y(:)], 1, [n n]) ;</p>
<p>On my machine, the for i=1:1000 code above using sparse inside the for loop takes 21.5 seconds, your sub2ind code takes 1.3 seconds, and the above code takes 0.2 seconds.  The above code also uses far less memory than your sub2ind code, too.</p>
<p>I find that it&#8217;s often faster to avoid fancy indexing, and do as many whole-matrix operations as possible (such as the above accumarray).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, P.E.</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16323</link>
		<dc:creator>Oliver A. Chapman, P.E.</dc:creator>
		<pubDate>Mon, 30 Jul 2007 02:35:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16323</guid>
		<description>Luca,

I agree that MatLab&#039;s documentation does adequately explain the convention for mapping a vector of indices into a variable of higher dimensions.  However, some people who read my MatLab code may not have read that section of MatLab documentation, so it won&#039;t be clear to them how this works.  That is why I avoid this technique.  As Dykstra said, &quot;. . . it is not only the programmer&#039;s task to produce a correct program but also to demonstrate its correctness in a convincing manner . . . &quot;</description>
		<content:encoded><![CDATA[<p>Luca,</p>
<p>I agree that MatLab&#8217;s documentation does adequately explain the convention for mapping a vector of indices into a variable of higher dimensions.  However, some people who read my MatLab code may not have read that section of MatLab documentation, so it won&#8217;t be clear to them how this works.  That is why I avoid this technique.  As Dykstra said, &#8220;. . . it is not only the programmer&#8217;s task to produce a correct program but also to demonstrate its correctness in a convincing manner . . . &#8220;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piotr</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16322</link>
		<dc:creator>Piotr</dc:creator>
		<pubDate>Fri, 27 Jul 2007 16:20:34 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16322</guid>
		<description>Urs,
You are right that accumarray can be used for adding to non zero matrices, but it is the slowest solution. Sparse is a little better but not much. For my problem I came up with much faster solution, however requiring additional memory (is&#039;nt it always true?)

My problem can be simulated by:

n=1000;k=50;
a=zeros(n,n);
for i=1:1000
    x=randsample(n,k,true);
    y=randsample(n,k,true);
%   a=a+accumarray([x,y],1,[n n]);
   a=a+sparse(x,y,1,n,n);
    
end

using accumarray cputime is ~34s
using sparse     it is   ~21s

adding another index to matrix a to deal with repetitions
I could get the code running 10 times faster:

n=1000;k=50;
a=zeros(n,n,k);
for i=1:1000
    x=randsample(n,k,true);
    y=randsample(n,k,true);
    l=sub2ind([n n k],x&#039;,y&#039;,1:k);
    a(l)=a(l)+1;
end
a=sum(a,3);


it runs in 2s.
My real problem has loop closer to 100,000 so time savings
are significant.</description>
		<content:encoded><![CDATA[<p>Urs,<br />
You are right that accumarray can be used for adding to non zero matrices, but it is the slowest solution. Sparse is a little better but not much. For my problem I came up with much faster solution, however requiring additional memory (is&#8217;nt it always true?)</p>
<p>My problem can be simulated by:</p>
<p>n=1000;k=50;<br />
a=zeros(n,n);<br />
for i=1:1000<br />
    x=randsample(n,k,true);<br />
    y=randsample(n,k,true);<br />
%   a=a+accumarray([x,y],1,[n n]);<br />
   a=a+sparse(x,y,1,n,n);</p>
<p>end</p>
<p>using accumarray cputime is ~34s<br />
using sparse     it is   ~21s</p>
<p>adding another index to matrix a to deal with repetitions<br />
I could get the code running 10 times faster:</p>
<p>n=1000;k=50;<br />
a=zeros(n,n,k);<br />
for i=1:1000<br />
    x=randsample(n,k,true);<br />
    y=randsample(n,k,true);<br />
    l=sub2ind([n n k],x&#8217;,y&#8217;,1:k);<br />
    a(l)=a(l)+1;<br />
end<br />
a=sum(a,3);</p>
<p>it runs in 2s.<br />
My real problem has loop closer to 100,000 so time savings<br />
are significant.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Urs (us) Schwarz</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16321</link>
		<dc:creator>Urs (us) Schwarz</dc:creator>
		<pubDate>Thu, 26 Jul 2007 23:07:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16321</guid>
		<description> The solution with accumarray will work only in case your initial matrix is zero...

in which case you would simply do this

     a=magic(5);
     loc=[1 1 3 3 1;1 3 1 3 1];
     data=pi*(1:size(loc,2));
     as=a+accumarray(loc.&#039;,data,size(a));
     a
     as
     (as-a)./pi

us</description>
		<content:encoded><![CDATA[<p>The solution with accumarray will work only in case your initial matrix is zero&#8230;</p>
<p>in which case you would simply do this</p>
<p>     a=magic(5);<br />
     loc=[1 1 3 3 1;1 3 1 3 1];<br />
     data=pi*(1:size(loc,2));<br />
     as=a+accumarray(loc.&#8217;,data,size(a));<br />
     a<br />
     as<br />
     (as-a)./pi</p>
<p>us</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piotr</title>
		<link>http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16320</link>
		<dc:creator>Piotr</dc:creator>
		<pubDate>Thu, 26 Jul 2007 21:58:12 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/#comment-16320</guid>
		<description>Hi,
The solution with accumarray will work only in case your initial matrix is zero. For more general problem of
A(loc)=A(loc)+data, where A is non zero at the begining
I think the solution would be to use sparse:

loc=[1 1 3 3 1;1 3 1 3 1];
a=a+sparse(loc(1,:),loc(2,:),data,3,3);

Piotr</description>
		<content:encoded><![CDATA[<p>Hi,<br />
The solution with accumarray will work only in case your initial matrix is zero. For more general problem of<br />
A(loc)=A(loc)+data, where A is non zero at the begining<br />
I think the solution would be to use sparse:</p>
<p>loc=[1 1 3 3 1;1 3 1 3 1];<br />
a=a+sparse(loc(1,:),loc(2,:),data,3,3);</p>
<p>Piotr</p>
]]></content:encoded>
	</item>
</channel>
</rss>

