Matlab函数accumarray的用法

Matlab函数accumarray的用法

 

 

碰到accumarray 这个函数是在Steve的文章[1 ]中。

 

 

 

这是一个很灵活的build-in函数,所以是没有m文件,看不到实现代码的。

 

 

 

开始理解起来比较拗,看了看例子知道什么意思了。

 

 

 

Matlab的Help [2 ]里这么说:

 

accumarray groups elements from a data set and applies a function to each group. A = accumarray(subs,val) creates an array A by accumulating elements of the vector val using the elements of subs as indices. The position of an element in subs determines which value of vals it selects for the accumulated vector; the value of an element in subs determines the position of the accumulated vector in the output.

 

对于A = accumarray(subs,val)这么一个调用,有这么几个问题,理解清楚,就理解了这个函数。

 

例子:

 

val    =     [     1     2     3     4     5    ]

subs =     [     1     2     4     2     4    ]'  % subs要是列向量
  

 

Q: accumarray总体是干嘛的?

 

A: 笼统的说,是用subs向量中的信息从val中提取数值做累加,累加完的结果放到A中。

 

 

 

Q: subs是干嘛的?

 

A: subs是一个累加指示向量。

 

subs提供的信息由两个:

 

(a). subs向量中的每个位置对应val的每个位置;

 

(b). subs中元素值相同的,val中的对应元素累加,元素值是累加完后放到A的什么地方。

 

       如:上面的例子中,subs(2),subs(4)都是2,所以,val(2)和val(4)累加起来,放到A(2)这个位置上。

 

 

 

Q: val是干嘛的?

 

A: val是提供累加数值的,谁累加呢?就是A中的数值累加。选哪些数进行累加呢?subs向量中数值相同的对应位置的数。累加完后放到哪里呢?放到subs中指示的位置。

 

 

 

Q: A是怎么出来的?A的维度是什么?A的内容如何确定?

 

A: A的维度是subs中表示维度的数值最大的那个,如例子中size(A,1)==4,因为max(subs)==4。当然,这只是一维的情况。

 

最后A的结果就是:

 

[c-sharp] view plain copy
  1. A =  
  2.      1   % subs(1)==1,所以,A(1) = val(1)。  
  3.      6   % subs(2)==subs(4)==2,所以,A(2)=val(2)+val(4)  
  4.      0   % subs中没有数值是3的值,也就是说A(3)上不累加任何数值  
  5.          % val(3)对应的subs(3)==4,所以val(3)累加到A(4)上去了  
  6.      8   % subs(3)==subs(5)==4,所以,A(4)=val(3)+val(5)  

 

 

 

 

 

Steve中用accumarray实现了一个从坐标对儿中统计位置的功能:

 

[c-sharp] view plain copy
  1. pairs = [...  
  2.     1 3; 1 2;  
  3.     2 1; 2 4;  
  4.     3 1; 3 4;  
  5.     4 2; 4 3;  
  6.     5 6; 5 7;  
  7.     6 5; 6 8;  
  8.     7 5; 7 8; 7 9;  
  9.     8 6; 8 7; 8 10;  
  10.     9 7; 9 10;  
  11.     10 8; 10 9]  
  12. A = accumarray(pairs, 1)  
  13. 结果:  
  14. A =  
  15.      0     1     1     0     0     0     0     0     0     0  
  16.      1     0     0     1     0     0     0     0     0     0  
  17.      1     0     0     1     0     0     0     0     0     0  
  18.      0     1     1     0     0     0     0     0     0     0  
  19.      0     0     0     0     0     1     1     0     0     0  
  20.      0     0     0     0     1     0     0     1     0     0  
  21.      0     0     0     0     1     0     0     1     1     0  
  22.      0     0     0     0     0     1     1     0     0     1  
  23.      0     0     0     0     0     0     1     0     0     1  
  24.      0     0     0     0     0     0     0     1     1     0  

 

 

 

 

 

这是accumarray一种灵活的用法。

 

 

 

可以非常高效地用统计一个矩阵中不重复的数值有哪些,见rocwoods的帖子 [3 ]。

 

 

 

太晚了,简单写一下,还有很多细节问题没有列出来:二维甚至更高维的应用,除了sum还可以自己定义函数处理等等,知道大概意思之后,可以仔细看Matlab的Help[2 ]了。

 

References:

 

[1] Connected component labeling - Part 3 | Steve on Image Processing,http://blogs.mathworks.com/steve/2007/03/20/connected-component-labeling-part-3/

 

[2] accumarray - The MathWorks,http://www.mathworks.com/help/techdoc/ref/accumarray .html

 

[3] 向量化操作的又一重要函数accumarray的用法总结,http://www.simwe.com/forum/thread-811616-1-3.html 

 

posted @ 2018-05-18 16:28  菜鸡一枚  阅读(3042)  评论(0编辑  收藏  举报