accumarray 输出的解释

Explanation of the output of accumarray

我刚刚阅读了 accumarray 的文档,无法理解第二个示例。例子如下

val = 101:106';
subs = [1 1; 2 2; 3 2; 1 1; 2 2; 4 1]
subs =

     1     1
     2     2
     3     2
     1     1
     2     2
     4     1
A = accumarray(subs,val)
A =

   205     0
     0   207
     0   103
   106     0

如果我这样做

B=accumarray(subs(:,1),val)
C=accumarray(subs(:,2),val)

然后我得到

B=

   205
   207
   103
   106

C =

   311
   310

这对我来说是合乎逻辑的。但是,当我将第二列添加到 subs?

摘自 accumarry 的 matlab 文档(注:以下引用来自 R2012a 文档,与 current version 不完全匹配)

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.

因此在您的示例中,'random' 排序来自 subs 指定的位置。分解 sub 的含义和最终结果我们得到这样的东西:

val = 101:106';
subs = [1 1; 2 2; 3 2; 1 1; 2 2; 4 1]
subs =

     1     1    <-- take val(1) which is 101 and put it at position [1, 1] in the output
     2     2    <-- put 102 in position [2, 2]
     3     2    <-- put 103 in position [3, 2]
     1     1    <--- ...and so on
     2     2
     4     1
A = accumarray(subs,val)
A =

   205     0    <--- [1, 1] has value 101+104, [1, 2] has no value
     0   207    <--- [2, 1] has no value, [2, 2] has value 102+105
     0   103    <--- ...and so on
   106     0