MATLAB Answers

What is the best way to count the occurrences of two table columns together?

1 ビュー (過去 30 日間)
Monika Jaskolka
Monika Jaskolka 2021 年 3 月 17 日
コメント済み: Monika Jaskolka 2021 年 3 月 17 日
I have a table that looks like this:
>> A = {'A'; 'B'; 'C'; 'A'; 'C'; 'C'; 'B'};
>> B = {'x'; 'y'; 'z'; 'xx'; 'z'; 'z'; 'y'};
>> T = table(A,B)
T =
A B
___ ____
'A' 'x'
'B' 'y'
'C' 'z'
'A' 'xx'
'C' 'z'
'C' 'z'
'B' 'y'
What is the best way of getting a result like the one blelow? I essentially want to count the occurrances of column A and B., and have as output a table.
ans =
A B occ
____ ____ ____
'A' 'x' 1
'A' 'xx' 1
'B' 'y' 2
'C' 'z' 3
The immediate solution that comes to mind is to combine the table columns together, and then use histc, but is there a better approach that will leave me with a Table where A and B aren't combined? Or is usng a strsplit on T2.AB the way to go?
>> c = strcat(T.A, ',', T.B)
c =
7×1 cell array
'A,x'
'B,y'
'C,z'
'A,xx'
'C,z'
'C,z'
'B,y'
>> [AB, ~, J] = unique(c);
>> occ = histc(J, 1:numel(AB));
>> T2 = table(AB, occ)
T2 =
AB occ
______ ___
'A,x' 1
'A,xx' 1
'B,y' 2
'C,z' 3

採用された回答

Star Strider
Star Strider 2021 年 3 月 17 日
Try this:
A = {'A'; 'B'; 'C'; 'A'; 'C'; 'C'; 'B'};
B = {'x'; 'y'; 'z'; 'xx'; 'z'; 'z'; 'y'};
T = table(A,B);
[Tu,~,ix] = unique(T, 'rows', 'stable');
Tally = accumarray(ix, 1);
TallyTable = [Tu, table(Tally)]
producing:
TallyTable =
4×3 table
A B Tally
_____ ______ _____
{'A'} {'x' } 1
{'B'} {'y' } 2
{'C'} {'z' } 3
{'A'} {'xx'} 1
The accumarray function is quite versatile, and applicable here.

その他の回答 (2 件)

Steve Eddins
Steve Eddins 2021 年 3 月 17 日
In my previous answer, I forgot that grpstats is in the Statistics and Machine Learning Toolbox. Here is an answer using groupsummary, which is in MATLAB.
>> groupsummary(T,["A" "B"])
ans =
4×3 table
A B GroupCount
___ ____ __________
"A" "x" 1
"A" "xx" 1
"B" "y" 2
"C" "z" 3
  1 件のコメント
Monika Jaskolka
Monika Jaskolka 2021 年 3 月 17 日
Very interesting function! Unfortunately for my purposes I need something that will work before R2018a, but this is good to know.

サインインしてコメントする。


Steve Eddins
Steve Eddins 2021 年 3 月 17 日
I think grpstats will do what you want:
>> grpstats(T,["A" "B"])
ans =
4×3 table
A B GroupCount
_____ ______ __________
A_x {'A'} {'x' } 1
A_xx {'A'} {'xx'} 1
B_y {'B'} {'y' } 2
C_z {'C'} {'z' } 3
Also, tabular text analysis will generally be more efficient and easier to perform if you work with strings instead of cell arrays of char vectors:
>> A = ["A" ; "B" ; "C" ; "A" ; "C" ; "C" ; "B" ]
A =
7×1 string array
"A"
"B"
"C"
"A"
"C"
"C"
"B"
>> B = ["x" ; "y" ; "z" ; "xx" ; "z" ; "z" ; "y" ]
B =
7×1 string array
"x"
"y"
"z"
"xx"
"z"
"z"
"y"
>> T = table(A,B)
T =
7×2 table
A B
___ ____
"A" "x"
"B" "y"
"C" "z"
"A" "xx"
"C" "z"
"C" "z"
"B" "y"
>> grpstats(T,["A" "B"])
ans =
4×3 table
A B GroupCount
___ ____ __________
A_x "A" "x" 1
A_xx "A" "xx" 1
B_y "B" "y" 2
C_z "C" "z" 3
  1 件のコメント
Steve Eddins
Steve Eddins 2021 年 3 月 17 日
Note that grpstats is in the Statistics and Machine Learning Toolbox. I posted a separate answer using groupsummary, which is a newer function that is in MATLAB.

サインインしてコメントする。

タグ

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by