Split and count unique string in cell array

조회 수: 8 (최근 30일)
Josipe Jurcic
Josipe Jurcic 2022년 3월 25일
댓글: Josipe Jurcic 2022년 3월 25일
I have a cell array in the form of:
A =
B25
A35
L35 J23
K32 I25
B25 ...
where cetain elements repeat. I need to count how many unique elements there are and then list number of occurences of each element. For the above example it would be something like:
B25 ... 2
L35 ... 1
K32 ... 1 etc.
I tried using different combinations of strplit, regexp and unique, but some returned errors, others returned an array with the whole row counted as unique, so for the example above it would say there are 4 unique elements instead of 6 because L35 J23 is counted as 1, not 2. There is a hint that converting to categorical might help, but I am not sure how to utilize its functions in order to get the desired result.

채택된 답변

Stephen23
Stephen23 2022년 3월 25일
A = {'B25';'A35';'L35 J23';'K32 I25';'B25'};
B = regexp(A,'\S+','match');
T = cell2table([B{:}].');
S = groupsummary(T,'Var1')
S = 6×2 table
Var1 GroupCount _______ __________ {'A35'} 1 {'B25'} 2 {'I25'} 1 {'J23'} 1 {'K32'} 1 {'L35'} 1
  댓글 수: 1
Josipe Jurcic
Josipe Jurcic 2022년 3월 25일
Thanks for your reply.
This one seems to do it. Thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Mohammed Hamaidi
Mohammed Hamaidi 2022년 3월 25일
A loop solution:
C=unique(A);nc=length(C);
B=char(A);nb=length(B);
D=zeros(nc,1);
for i=1:nc
for j=1:nb
if strcmp(B(j,:),char(C{i}))
D(i)=D(i)+1;
end
end
end
for i=1:nc
disp([char(C{i}) ' ' num2str(D(i))])
end
  댓글 수: 1
Josipe Jurcic
Josipe Jurcic 2022년 3월 25일
Thanks for your reply.
It throws this error:
Index in position 1 exceeds array bounds. Index must not exceed 6.

댓글을 달려면 로그인하십시오.


Simon Chan
Simon Chan 2022년 3월 25일
Use function groupsummary
A = {'B25';'A35';'L35 J23';'K32 I25';'B25'};
T = table(A);
groupsummary(T,'A')
ans = 4×2 table
A GroupCount ___________ __________ {'A35' } 1 {'B25' } 2 {'K32 I25'} 1 {'L35 J23'} 1
  댓글 수: 3
Simon Chan
Simon Chan 2022년 3월 25일
Just add a few things as follows:
A = {'B25';'A35';'L35 J23';'K32 I25';'B25'; 'L35 J10'};
B = cellfun(@(x) strsplit(x),A,'uni',0); % Split them
C = cat(2,B{:})'; % Combine as a column
T = table(C);
groupsummary(T,'C')
ans = 7×2 table
C GroupCount _______ __________ {'A35'} 1 {'B25'} 2 {'I25'} 1 {'J10'} 1 {'J23'} 1 {'K32'} 1 {'L35'} 2
Josipe Jurcic
Josipe Jurcic 2022년 3월 25일
Thanks for your reply.
This works as well. Thanks again.

댓글을 달려면 로그인하십시오.

카테고리

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