how do i count unique cell rows?

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 件のコメント

dpb
dpb 2019 年 12 月 7 日
Give a short(ish) sample section of the data...
If understand the Q?, doc unique with 'rows' option should get you started...
Kangkeon Jeon
Kangkeon Jeon 2019 年 12 月 7 日
each cell can be one of these three : 'AA', 'AB', 'BB'
so possible rows are
'AA' 'AA'
'AA' 'AB'
'AA' 'BB'
'AB' 'AA'
'AB' 'AB'
'AB' 'BB'
'BB' 'AA'
'BB' 'AB'
'BB' 'BB'
i already used unique(cell2mat(total_people),'rows') to find all possible options but i don't know how to run a loop to count how many of each unique rows.
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 日

1 投票

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 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2019 年 12 月 7 日

編集済み:

dpb
2019 年 12 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by