Filter löschen
Filter löschen

Outputting cumulative sum for Cell array with strings and numbers

2 Ansichten (letzte 30 Tage)
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...

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 1 Jul. 2015
Bearbeitet: Andrei Bobrov am 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))))];

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by