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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by