Matlab - 数组中出现最多的值(不只是众数)

4

我有一个数字数组,范围从1到4。我需要知道重复最多的数值是哪些。如果有平局,我需要知道这些数值,以便进行一些操作。

例子:

a = [1 1 1 2 2 2 3 4]
Output = [1 2]

a = [1 1 1 2 3 4]
Output = 1

a = [1 2 2 3 3 4 4]
Output = [2 3 4]

有什么想法吗?

你的输入变量 a 是否总是已排序的? - Luis Mendo
5个回答

5

使用histunique的替代向量化方法

uVal = unique(a);
counts = hist(a,uVal);
out = uVal(counts == max(counts));

结果:

a = [1 1 1 2 2 2 3 4];

>> out

out =

 1     2

4
假设输入的行向量已经排序(否则可以使用 sort() 函数进行排序),您可以使用基于 finddiffmax 的方法。
%// Find starting indices of each island of identical numbers being
%// appended by the numel()+1 with the intention of getting island lengths
%// later on by differentiating along the indices
start_ind = [0 find(diff(a)) numel(a)]+1
lengths = diff(start_ind)

%// Look for the islands with the max island lengths. 
%// Use those to get unique numbers associated with them for final output
out = a(start_ind([lengths == max(lengths) false]))

基准测试

以下是一个基准测试,用于比较迄今为止列出的四种解决方案的运行时间 -

a = randi(10000,1,1000000);

disp('---------------- With for-loop')
tic
values = unique(a);
counts = zeros(1,numel(values));
for i=1:numel(values)
    counts(i) = sum(a == values(i));
end
output = values(counts == max(counts));
toc
clear output counts values

disp('---------------- With find+diff+max')
tic
sa = sort(a);
start_ind = [0 find(diff(sa)) numel(sa)]+1;
lengths = diff(start_ind);
out = sa(start_ind([lengths == max(lengths) false]));
toc
clear out lengths start_ind sa

disp('---------------- With mod')
tic
[~, ~, v] = mode(a);
result = v{1};
toc
clear v result

disp('---------------- With unique+hist+max')
tic
uVal = unique(a);
counts = hist(a,uVal);
out = uVal(counts == max(counts));
toc

运行时 -

---------------- With for-loop
Elapsed time is 32.879074 seconds.
---------------- With find+diff+max
Elapsed time is 0.077948 seconds.
---------------- With mod
Elapsed time is 0.136005 seconds.
---------------- With unique+hist+max
Elapsed time is 0.250994 seconds.

1
哈哈哈,最长的时间是 for 循环,太棒了。+1。 - rayryeng

4
mode 的第三个输出正是那个。输入向量不需要排序。
[~, ~, v] = mode(a);
result = v{1};

3
您可以像这样做:
a = [1 1 1 2 2 2 3 4];
values = [1 2 3 4];

counts = zeros(1,numel(values));

for i=1:numel(values)
    counts(i) = sum(a == values(i));
end

output = values(counts == max(counts));

2
你也可以使用 accumarray 结合 unique 来实现:
[vals,~,id] = unique(a);
b = accumarray(id, 1);
result = vals(b == max(b));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接