matlab数组重复复制,Matlab - 数组中的大多数重复值(不仅仅是模式)

假设输入行向量已经排序(否则您可以通过调用sort()对其进行排序),您可以使用基于 find , diff & max 的方法 -

%// 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值