如何在matlab中计算单元的唯一元素?

如何在matlab中计算单元的唯一元素?,matlab,Matlab,我想在Matlab中计算单元数组的唯一元素。我该怎么做?多谢各位 c = {'a', 'b', 'c', 'a'}; % count unique elements, return the following struct unique_count.a = 2 unique_count.b = 1 unique_count.c = 1 要计算唯一元素,可以与 要生成结构,请使用和: 在文件交换上签出。它使用accumarray或sort,具体取决于哪种方法最合适。它还将检查NAN/INF。它可

我想在Matlab中计算单元数组的唯一元素。我该怎么做?多谢各位

c = {'a', 'b', 'c', 'a'};
% count unique elements, return the following struct
unique_count.a = 2
unique_count.b = 1
unique_count.c = 1

要计算唯一元素,可以与

要生成结构,请使用和:


在文件交换上签出。它使用
accumarray
sort
,具体取决于哪种方法最合适。它还将检查NAN/INF。

它可以计数吗?那么数字和字符的组合呢?
c = {'a', 'b', 'c', 'a'};
[uniqueC,~,idx] = unique(c); %# uniqueC are unique entries in c
                             %# replace the tilde with 'dummy' if pre-R2008a

counts = accumarray(idx(:),1,[],@sum); 
countCell = num2cell(counts);
tmp = [uniqueC;countCell']; %'

unique_count = struct(tmp{:}) %# this evaluates to struct('a',2,'b',1,'c') 

unique_count = 
    a: 2
    b: 1
    c: 1