how do i count unique cell rows?
조회 수: 7 (최근 30일)
이전 댓글 표시
I have 10000x2 char cell i want to count how many of each unique rows there is.
There are 9 possible unique rows. How can i code so that it counts how many of those each 9 types are in the cell array?
댓글 수: 4
답변 (2개)
dpb
2019년 12월 7일
편집: dpb
2019년 12월 8일
I agree w/ Per, use strings since TMW didn't see fit to extend unique to cell arrays and either is far better than char() arrays for almost every purpose...
% build a sample dataset...
s=string({'AA' 'AA'
'AA' 'AB'
'AA' 'BB'
'AB' 'AA'
'AB' 'AB'
'AB' 'BB'
'BB' 'AA'
'BB' 'AB'
'BB' 'BB'});
s=repmat(s,5,1); % duplicate to have more than one of each possible
s=s(randperm(45,30),:); % pick a random subset so not all the same...
% preliminaries done, now for the work...
[~,ia,ic]=unique(s,'rows'); % return locations of which set for all rows in s
n=histc(ic,unique(ic)); % count the number in each bin...
For the particular randomized sample I ran here at command line the above returns--
>> [s(ia,:) unique(ic) n]
ans =
9×4 string array
"AA" "AA" "1" "3"
"AA" "AB" "2" "4"
"AA" "BB" "3" "3"
"AB" "AA" "4" "4"
"AB" "AB" "5" "4"
"AB" "BB" "6" "3"
"BB" "AA" "7" "3"
"BB" "AB" "8" "4"
"BB" "BB" "9" "2"
>>
where used the penchant for string class to turn everything it touches into strings to be able to display disparate types together...the unique string pairs followed by the bin number for each and then the count for the respective bin.
To verify results are correct, here's the particular s array...
>> s
s =
30×2 string array
"BB" "AB"
"AA" "AA"
"BB" "AB"
"AB" "AB"
"AB" "AA"
"AB" "AB"
"AA" "AB"
"AB" "AA"
"AA" "AB"
"AB" "BB"
"BB" "BB"
"AA" "BB"
"AA" "AB"
"BB" "AA"
"AA" "AB"
"BB" "AB"
"AA" "BB"
"BB" "AA"
"BB" "BB"
"AB" "AA"
"AB" "AB"
"AB" "BB"
"BB" "AA"
"AB" "AA"
"AB" "AB"
"AA" "AA"
"AA" "BB"
"AA" "AA"
"AB" "BB"
"BB" "AB"
>>
댓글 수: 0
Image Analyst
2019년 12월 7일
There are more compact (read cryptic) ways, but this simple and easy-to-understand (for a beginner) way using a for loop and switch statement works well:
% Define the cell array:
ca = {
'AA' 'AA'
'AA' 'AB'
'AA' 'BB'
'AB' 'AA'
'AB' 'AB'
'AB' 'BB'
'BB' 'AA'
'BB' 'AB'
'BB' 'BB'}
numRows = size(ca, 1);
counts = zeros(numRows, 1);
% Count how many of the unique strings there are
for row = 1 : size(ca, 1)
str = [ca{row, 1}, ca{row, 2}]
switch upper(str)
case 'AAAA'
counts(1) = counts(1) + 1; % Increment count for this patterm
case 'AAAB'
counts(2) = counts(2) + 1; % Increment count for this patterm
case 'AABB'
counts(3) = counts(3) + 1; % Increment count for this patterm
case 'ABAA'
counts(4) = counts(4) + 1; % Increment count for this patterm
case 'ABAB'
counts(5) = counts(5) + 1; % Increment count for this patterm
case 'ABBB'
counts(6) = counts(6) + 1; % Increment count for this patterm
case 'BBAA'
counts(7) = counts(7) + 1; % Increment count for this patterm
case 'BBAB'
counts(8) = counts(8) + 1; % Increment count for this patterm
case 'BBBB'
counts(9) = counts(9) + 1; % Increment count for this patterm
end
end
counts % Report to command window.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!