【问题标题】:Fastest way of finding and multiplying repeated values in array在数组中查找和相乘重复值的最快方法
【发布时间】:2015-03-30 14:04:19
【问题描述】:

在数组中查找和相乘重复值的最快方法是什么?

例子:

a = [ 2 2 3 5 11 11 17 ]

结果:

a = [ 4 3 5 121 17 ]

我可以想到迭代方法(通过查找历史数据、迭代 bin 等),但有没有矢量化/快速方法?

【问题讨论】:

  • 我刚刚看了其他答案:a = [2 2 3 3 2 2] 的预期输出是什么? Divakar 的答案是 [4,9,4],而 thewaywewalk 的答案是 [16,9]。
  • @knedlsepp 老实说,这不会发生。但是,好吧,选择你喜欢的那个。我会说第二个选项更适合我正在做的事情,但这并不重要。很高兴你注意到了。

标签: arrays matlab vectorization


【解决方案1】:

使用histc 和unique:

ua = unique(a)
out = ua.^histc(a,ua)

out =

     4     3     5   121    17

考虑到向量a不是单调递增的,它变得有点复杂:

%// non monotonically increasing vector
a = [ 2 2 3 5 11 11 17 4 4 1 1 1 7 7]

[ua, ia] = unique(a)             %// get unique values and sort as required for histc  
[~, idx] = ismember(sort(ia),ia) %// get original order
hc = histc(a,ua)                 %// count occurences
prods = ua.^hc                   %// calculate products
out = prods(idx)                 %// reorder to original order

或:

ua = unique(a,'stable')          %// get unique values in original order
uas = unique(a)                  %// get unique values sorted as required for histc  
[~,idx] = ismember(ua,uas)       %// get indices of original order
hc = histc(a,uas)                %// count occurences
out = ua.^hc(idx)                %// calculate products and reorder 

out =

     4     3     5   121    17    16     1    49

accumarray 和 doesn't offer a stable version by default 似乎仍然是一个很好的解决方案。

【讨论】:

  • 哦,好吧,Divakar 的回答很酷,但是这个回答又短又清晰!非常好。
  • 由于代码的长度和清晰度,我将接受这个答案,但是它们都非常好。
  • @AnderBiguri 看看我的编辑,我不知道它是否适用于你的情况。
  • @Divakar 感谢您的建议!我误用了排序......我现在偷了你的 ismember 方法。无论如何,我的赞成票你已经得到了:)
  • 哦。我猜问题出在以下:同一整数的多次非连续出现:a = [2 2 3 3 2 2].
【解决方案2】:

前瞻性方法和解决方案代码

似乎发布的问题很适合accumarray -

%// Starting indices of each "group"
start_ind = find(diff([0 ; a(:)]))

%// Setup IDs for each group
id = zeros(1,numel(a)) %// Or id(numel(a))=0 for faster pre-allocation
id(start_ind) = 1

%// Use accumarray to get the products of elements within the same group
out = accumarray(cumsum(id(:)),a(:),[],@prod)

对于非单调递增的输入,需要再添加两行代码-

[~,sorted_idx] = ismember(sort(start_ind),start_ind)
out = out(sorted_idx)

示例运行 -

>> a
a =
     2     2     3     5    11    11    17     4     4     1     1     1     7     7
>> out.'
ans =
     4     3     5   121    17    16     1    49

扭扭捏捏的

现在,可以使用 logical indexing 删除 find 并使用 更快的预分配方案,为提议的方法提供超级提升,并为我们提供调整后的代码 -

id(numel(a))=0;
id([true ; diff(a(:))~=0])=1;
out = accumarray(cumsum(id(:)),a(:),[],@prod);

基准测试

这是比较迄今为止针对运行时所述问题发布的所有建议方法的基准测试代码 -

%// Setup huge random input array
maxn = 10000;
N = 100000000;
a = sort(randi(maxn,1,N));

%// Warm up tic/toc.
for k = 1:100000
    tic(); elapsed = toc();
end

disp('------------------------- With UNIQUE')
tic
ua = unique(a);
out = ua.^histc(a,ua);
toc, clear ua out

disp('------------------------- With ACCUMARRAY')
tic
id(numel(a))=0;
id([true ; diff(a(:))~=0])=1;
out = accumarray(cumsum(id(:)),a(:),[],@prod);
toc, clear out id

disp('------------------------- With FOR-LOOP')
tic
b = a(1);
for k = 2:numel(a)
    if a(k)==a(k-1)
        b(end) = b(end)*a(k);
    else
        b(end+1) = a(k);
    end
end
toc

运行时

------------------------- With UNIQUE
Elapsed time is 3.050523 seconds.
------------------------- With ACCUMARRAY
Elapsed time is 1.710499 seconds.
------------------------- With FOR-LOOP
Elapsed time is 1.811323 seconds.

结论:看起来,运行时支持accumarray的想法优于其他两种方法!

【讨论】:

  • 我期待着 Divakar 给我这样的好回答!
  • @AnderBiguri 我也是! ;)
  • @Divakar:你运行的是什么版本的 MATLAB?复制粘贴你的代码我得到完全不同的结果。 (Macbook Pro 4GB RAM,R2014a):unique:12.3 秒,accumarray:9.8 秒,for:2.9 秒。
  • 看来N=1e8 对于我的机器来说对于accumarray 来说内存太多了。 N=1e7 结果更相似,但 for 循环仍然比 accumarray 这里:unique:0.94s,accumarray:0.31s,for:0.26s。
  • @knedlsepp 我在 2015A,16GB RAM。让它N = 1000000和maxn = 100?我们的想法是使用 tic-toc 导致超过 1 秒运行时间的此类数据大小,从而减少使用 tic-toc 产生的变化的影响。
【解决方案3】:

您可能会惊讶于简单的for-loop 在速度方面的对比:

b = a(1);
for k = 2:numel(a)
    if a(k)==a(k-1)
        b(end) = b(end)*a(k);
    else
        b(end+1) = a(k);
    end
end

即使不进行任何预分配,它的性能也与accumarray 解决方案相当。

【讨论】:

  • 添加了一些调整和基准以获得更多惊喜 :) 不错的替代解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 2015-02-17
  • 2015-12-13
相关资源
最近更新 更多