How do you identify strings with numbered names in an array?

조회 수: 2 (최근 30일)
Matthew
Matthew 2019년 3월 6일
편집: madhan ravi 2019년 3월 6일
Hello,
I have a list of data that I want to simplify. The data in column 1 is of the format "(part) #". Each part will have various numbers associated with it and I want to add the number in column 2 for each part entry.
The "part" name can have a number within it and the first appearance of each part will not contain a "#" entry.
For example, my data can look like this:
Column 1 | Column 2
Default | 8
Default 2 | 9
Default 3 | 3
Default 4 | 5
Card 1 | 6
Card 1 2 | 10
Card 1 3 | 3
Card 1 4 | 2
Card 1 5 | 9
Card 1 6 | 3
Card 2 | 2
Card 2 2 | 2
Card 2 3 | 6
Card 2 4 | 8
Card 2 5 | 6
Card 2 6 | 9
I would like to output this:
Default | 25
Card 1 | 33
Card 2 | 33
I'm not sure how to get matlab to 1. recognize that 'Default', 'Card 1', and 'Card 2' are the things I want to perform operations to, and 2. recognize that I need it to sum over how many times the part is repeated.
Thanks in advance for any assistance you can offer.
Matt

채택된 답변

Alex Mcaulley
Alex Mcaulley 2019년 3월 6일
a= {'Default ',2; 'Default 2',4; 'Card 1 2',6};
res = {'Default '; 'Card 1 '};
res{1,2} = sum(cell2mat(a(startsWith(a(:,1),res(1,1)),2)));
res{2,2} = sum(cell2mat(a(startsWith(a(:,1),res(2,1)),2)));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 C2000 Microcontroller Blockset에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by