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

MATLAB

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,

Best Answer

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')