필터 지우기
필터 지우기

Outputting cumulative sum for Cell array with strings and numbers

조회 수: 2 (최근 30일)
Eli Dim
Eli Dim 2015년 7월 1일
댓글: Eli Dim 2015년 7월 2일
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