フィルターのクリア

How to count the number of unique elements by group in a table

20 ビュー (過去 30 日間)
Blue
Blue 2019 年 8 月 8 日
回答済み: Blue 2019 年 8 月 8 日
Hi,
How can I count the number of unique elements by group in a table and save the answer in a vector ?
% Create table
name = {'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'}.';
type = {'AA', 'BB', 'CC', 'BB', 'BB', 'BB', 'AA', 'CC', 'CC'}.';
t = table(name, type);
For example, in the table here the number of unique 'type' per 'name' would yield 3, 1, 2 (i.e. 3 'unique 'types' for name A, 1 'unique 'types' for name B, and 2 'unique 'types' for name C,)
Thank you,

採用された回答

Blue
Blue 2019 年 8 月 8 日
name = {'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'}.';
type = {'AA', 'BB', 'CC', 'BB', 'BB', 'BB', 'AA', 'CC', 'CC'}.';
t = table(name, type);
n_type = rowfun(@(v) numel(unique(v)), t, 'GroupingVariables', 'name', 'InputVariables', 'type')

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 8 月 8 日
編集済み: KALYAN ACHARJYA 2019 年 8 月 8 日
name = {'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'}.';
type = {'AA', 'BB', 'CC', 'BB', 'BB', 'BB', 'AA', 'CC', 'CC'}.';
t = table(unique(name), unique(type))
Command Window:
t =
3×2 table
Var1 Var2
_ ____
'A' 'AA'
'B' 'BB'
'C' 'CC'
Unique Elements
>> H=height(t)
H =
3
  1 件のコメント
Blue
Blue 2019 年 8 月 8 日
Thank you kindly for your answer but it doesnt quite do what I want. I need a count of the unique elements per group, in this case 3,1,2.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by