Time series aggregation efficiency

前端 未结 5 893
傲寒
傲寒 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:16

    Well I have a solution that is almost as quick as the mex but only using matlab. The logic is the same as most of the above, creating a dummy 2D matrix but instead of using @eq I initialize a logical array from the start.

    Elapsed time for mine is 0.172975 seconds. Elapsed time for Divakar 0.289122 seconds.

    function aggArray = aggregate(array, group, collapseFn)
        [m,~] = size(array);
        n = max(group);
        D = false(m,n); 
        row = (1:m)';
        idx = m*(group(:) - 1) + row;
        D(idx) = true;
        out = zeros(m,size(array,2));
        for ii = 1:n
            out(ii,:) = collapseFn(array(D(:,ii),:),1);
        end
    end
    

提交回复
热议问题