Merging Unique Cell Array Elements

2 ビュー (過去 30 日間)
matuser123
matuser123 2016 年 9 月 9 日
コメント済み: Star Strider 2016 年 9 月 9 日
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]

採用された回答

Star Strider
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.
  2 件のコメント
matuser123
matuser123 2016 年 9 月 9 日
Thanks!
Star Strider
Star Strider 2016 年 9 月 9 日
My pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by