× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Accumarray with non-scalar subscripts in Matlab?

Ray Phan
Jul 02, 2015
<p>Use the third output of <code>unique</code> when examining <code>A</code> over the rows and with the <code>'stable'</code> flag, then use this as input into <code>accumarray</code>. The third output assigns a unique ID for each unique occurrence of a row vector seen in your matrix <code>A</code>, which is very suitable for use in <code>accumarray</code>:</p> <pre><code>[~,~,f] = unique(A(:,2:end), 'rows', 'stable'); B = accumarray(f, A(:,1)); </code></pre> <p>We get:</p> <pre><code>B = -3 %// 2-5 5 %// 3+2 -2 %// -8+6 11 %// 7+3+1 -3 %// 3-8+2 9 %// 1+1+7 14 %// 5+0+9 </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31075957/Accumarray%20with%20non-scalar%20subscripts%20in%20Matlab?/31076490">Stack Overflow</a>.</p>
comments powered by Disqus