Time series aggregation efficiency

前端 未结 5 899
傲寒
傲寒 2020-12-20 15:46

I commonly need to summarize a time series with irregular timing with a given aggregation function (i.e., sum, average, etc.). However, the current solution that I have seem

5条回答
  •  爱一瞬间的悲伤
    2020-12-20 16:24

    A little late to the party, but a single loop using accumarray makes a huge difference:

    function aggArray = aggregate_gnovice(array, groupIndex, collapseFn)
    
      [groups, ~, index] = unique(groupIndex, 'rows');
      numCols = size(array, 2);
      aggArray = nan(numel(groups), numCols);
      for col = 1:numCols
        aggArray(:, col) = accumarray(index, array(:, col), [], collapseFn);
      end
    
    end
    

    Timing this using timeit in MATLAB R2016b for the sample data in the question gives the following:

    original | 1.127141
     gnovice | 0.002205
    

    Over a 500x speedup!

提交回复
热议问题