Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个矩阵
i j values a = 1 2 10 1 2 20 2 1 11 2 2 10 2 1 1
我想根据前两列合并矩阵 a 的行,其中前两列具有相同的值。结果应该看起来像
资源=
1 2 30 2 1 12 2 2 10
没有循环可以做到这一点吗?
谢谢
检索前两列的唯一对,然后使用将所有行映射a到唯一对的索引,最后根据映射累积第三列中的值:
a
[un, ~, subs] = unique(a(:,1:2),'rows'); [un accumarray(subs,a(:,3))]