我有一个向量比方说
A = [1; 2; 3; 1; 2; 3; 1; 2; 3]
我怎样才能找到唯一的元素和它们的计数像这样?
1 = 3
2 = 3
3 = 3
我绝对可以循环遍历各个元素并使用一些简单的技术。我不想要这样的解决方案,因为我的实际数据将在数百万的范围内,所以我不能只循环遍历每个元素。
在matlab中最有效的方法是什么?
要想提高效率,很难比这更好了:
elements = unique(A);
counts = histc(A(:), elements);
不像这样的答案是正确的。但是它的效率(计算时间)实际上是可以提高的:
sA = sort([A(:); inf]); %// sort A
ind = diff(sA)~=0; %// index of last element of each run of equal values
elements = sA(ind); %// unique elements
counts = diff([0; find(ind)]); %// lengths of runs
基准测试:
clear all
A = randi(100,1e6,1); %// Example data. Large column vector
%// Notlikethat's answer
tic
elements = unique(A);
counts = histc(A(:), elements);
toc
clear elements counts
%// This answer
tic
sA = sort([A(:); inf]);
ind = diff(sA)~=0;
elements = sA(ind);
counts = diff([0; find(ind)]);
toc
Elapsed time is 0.175594 seconds.
Elapsed time is 0.076032 seconds.