Outputting cumulative sum for Cell array with strings and numbers

2 次查看(过去 30 天)
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...

采纳的回答

Andrei Bobrov
Andrei Bobrov 2015-7-1
编辑:Andrei Bobrov 2015-7-1
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))))];

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by