Outputting cumulative sum for Cell array with strings and numbers

1 view (last 30 days)
Cell1={
'A1' 5
'A2' 1
'A3' 2
'A4' 4
'A5' 1
'B1' 2
'B2' 6
'B3' 17
'B4' 8
'B5' 3
'C1' 7
'C2' 8
'D1' 1
.
.
.
};
I have a cell Cell1 which has varying row length. How can I say for example; for all As Bs Cs Ds Es etc .... count the values in column two. In the end, I would like to have an output like this
'A' 13
'B' 36
'C' 15
'D' 1
etc...

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 1 Jul 2015
Edited: Andrei Bobrov on 1 Jul 2015
Cell1={
'A1' 5
'A2' 1
'A3' 2
'A4' 4
'A5' 1
'B1' 2
'B2' 6
'B3' 17
'B4' 8
'B5' 3
'C1' 7
'C2' 8
'D1' 1};
S = regexp(Cell1(:,1),'^[A-Za-z]+','match');
S = [S{:}]';
[a,~,c] = unique(S);
out = [a num2cell(accumarray(c,cell2mat(Cell1(:,2))))];

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!