【问题标题】:How to increment some of elements in an array by specific values in MATLAB如何通过MATLAB中的特定值增加数组中的某些元素
【发布时间】:2016-03-23 09:58:09
【问题描述】:

假设我们有一个数组

A = zeros([1,10]);

我们有几个可能重复的索引:

indSeq = [1,1,2,3,4,4,4];

我们如何将A(i) 增加索引序列中i 的数量,即A(1) = 2, A(2) = 1, A(3) = 1, A(4) = 3

代码A(indSeq) = A(indSeq)+1 不起作用。

我知道我可以使用以下for循环来实现目标,但我想知道是否有任何方法可以避免for循环?我们可以假设indSeq 已排序。

for 循环解决方案:

for i=1:length(indSeq)
  A(indSeq(i)) = A(indSeq(i))+1;
end;

【问题讨论】:

    标签: arrays performance matlab vectorization run-length-encoding


    【解决方案1】:

    您可以将accumarray 用于此类基于标签的计数作业,就像这样 -

    accumarray(indSeq(:),1)
    

    基准测试

    按照other answer 中的建议,您也可以使用hist/histc。让我们针对大数据量对这两个进行基准测试。我使用的基准代码有 -

    %// Create huge random array filled with ints that are duplicated & sorted
    maxn = 100000;
    N = 10000000;
    indSeq = sort(randi(maxn,1,N));
    
    disp('--------------------- With HISTC')
    tic,histc(indSeq,unique(indSeq));toc
    
    disp('--------------------- With ACCUMARRAY')
    tic,accumarray(indSeq(:),1);toc
    

    运行时输出 -

    --------------------- With HISTC
    Elapsed time is 1.028165 seconds.
    --------------------- With ACCUMARRAY
    Elapsed time is 0.220202 seconds.
    

    【讨论】:

      【解决方案2】:

      这是运行长度编码,下面的代码应该可以为您解决问题。

      A=zeros(1,10);
      indSeq = [1,1,2,3,4,4,4,7,1];
      indSeq=sort(indSeq); %// if your input is always sorted, you don't need to do this
      pos = [1; find(diff(indSeq(:)))+1; numel(indSeq)+1];
      A(indSeq(pos(1:end-1)))=diff(pos)
      

      返回

      A =
           3     1     1     3     0     0     1     0     0     0
      

      此算法由 Luis Mendo 为 MATL 编写。

      【讨论】:

        【解决方案3】:

        我认为您正在寻找的是数组唯一值的出现次数。这可以通过以下方式完成:

        [num, val] = hist(indSeq,unique(indSeq));
        

        你的例子的输出是:

        num = 2 1 1 3
        val = 1 2 3 4
        

        所以 num 是 val 出现的次数。即数字 1 在您的示例中出现 2 次​​p>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-12-12
          • 1970-01-01
          • 2021-09-13
          • 2022-09-28
          • 2016-11-22
          • 2019-12-18
          • 2022-12-07
          相关资源
          最近更新 更多