【问题标题】:How can I sum specific values of a column corresponding to unique values of another column, without using the "accumarray" command?如何在不使用“accumarray”命令的情况下对与另一列的唯一值相对应的列的特定值求和?
【发布时间】:2014-05-12 23:01:08
【问题描述】:

我在matlab中有一个矩阵:

a=[1 1; 1 2; 1 3; 2 1; 2 5; 2 7; 3 2; 3 1; 3 4];

如果

a1=[1 1 1 2 2 2 3 3 3]; is the first column

a2=[1 2 3 1 5 7 2 1 4]; is the second column

对于这个矩阵,我希望a1的重复值“unique(a1)”对a2的对应值求和,从而得到:

a3=[1+2+3 1+5+7 2+1+4]=[6 13 7] 

但不使用“accumarray”命令 有什么帮助吗?

【问题讨论】:

  • 每当有人提出问题时,请在后面加上“不使用 ______(明确用于此目的的工具)”,我想知道为什么......
  • @tmpearce:根据对我的回答的评论,我猜想 OP 使用的是较旧的 Matlab 版本。也许accumarray 没有在他的版本中实现?
  • 确实如此。是6.5版本

标签: matlab


【解决方案1】:

我的consolidator 工具会为您执行此操作,甚至提供公差。

[a1cons,a2cons] = consolidator(a1',a2',@sum)
a1cons =
     1
     2
     3

a2cons =
     6
    13
     7

【讨论】:

  • ...而且该工具向后兼容!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 2022-01-25
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
相关资源
最近更新 更多