Merging Unique Cell Array Elements
9 次查看(过去 30 天)
显示 更早的评论
I have a cell array with non unique elements and an array with corresponding integer values. I'd like to combine all the repeat cell array values and sum the corresponding array values. Any ideas?
% Old Data
descr = {'A','B','C','A','B'}
value = [2,4,6,-5,8]
% New Data
descr = {'A','B','C'}
value = [-3,12,6]
0 个评论
采纳的回答
Star Strider
2016-9-9
See if this does what you want:
descr = {'A','B','C','A','B'};
value = [2,4,6,-5,8];
[Uv,~,ref] = unique(descr);
acc = accumarray(ref, value');
Result = table(Uv', acc)
Result =
Var1 acc
____ ___
'A' -3
'B' 12
'C' 6
The table output is optional, and just shows the result.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!