how do i count unique cell rows?
11 次查看(过去 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 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!