Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我想计算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 回答: 要计算唯一元素,可以将UNIQUE与ACCUMARRAY结合使用 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); 要生成结构,请使用NUM2CELL和STRUCT : 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 更多&回答... |
![]() |
![]() |