how do i count unique cell rows?

조회 수: 9 (최근 30일)
Kangkeon Jeon
Kangkeon Jeon 2019년 12월 7일
편집: dpb 2019년 12월 8일
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
Kangkeon Jeon
Kangkeon Jeon 2019년 12월 7일
i thought about using
if total_people{i,1} == 'AA' && total_people{i,2} == 'AA'
AAAA = AAAA + 1;
end
in a for-loop like that to count each option but it gives
"Operands to the || and && operators must be convertible to logical scalar values." error
per isakson
per isakson 2019년 12월 7일
Doc says: The 'rows' option does not support cell arrays.

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

답변 (2개)

dpb
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"
>>

Image Analyst
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by