![]() |
铲斗算法
我有一些有效的代码,但是有点瓶颈,我一直在试图找出如何加快它的速度。这是一个循环,我无法弄清楚如何对其进行向量化。
我有一个2D数组vals,它代表时间序列数据。行是日期,列是不同的系列。我试图按月对数据进行存储分区以对其执行各种操作(求和,均值等)。这是我当前的代码: allDts; %Dates/times for vals. Size is [size(vals, 1), 1] vals; [YM] = datevec(allDts); fomDates = unique(datenum(Y, M, 1)); %first of the month dates [YM] = datevec(fomDates); nextFomDates = datenum(Y, M, DateUtil.monthLength(Y, M)+1); newVals = nan(length(fomDates), size(vals, 2)); %preallocate for speed for k = 1:length(fomDates); 下一行是瓶颈,因为我已经多次调用它了。 idx = (allDts >= fomDates(k)) & (allDts < nextFomDates(k)); bucketed = vals(idx, :); newVals(k, :) = nansum(bucketed); end %for 有任何想法吗?提前致谢。 回答: 这是一个很难向量化的问题。我可以建议一种使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cellfun.html"]CELLFUN的方法[/URL] ,但是我不能保证它会更快地解决您的问题(您必须根据要使用的特定数据集自行计时)。正如在[URL="https://stackoverflow.com/questions/831721/turning-a-matlab-binary-matrix-into-a-vector-of-the-last-nonzero-index-in-a-fast"]另一个SO问题中[/URL]所讨论的,矢量化并不[I]总是[/I]比for循环更快。这可能是非常特定于问题的,这是最佳选择。有了这个免责声明,我将为您推荐两种解决方案:CELLFUN版本和对for-loop版本的修改,可能会运行得更快。 [B]CELLFUN解决方案:[/B] [Y,M] = datevec(allDts); monthStart = datenum(Y,M,1); % Start date of each month [monthStart,sortIndex] = sort(monthStart); % Sort the start dates [uniqueStarts,uniqueIndex] = unique(monthStart); % Get unique start dates valCell = mat2cell(vals(sortIndex,:),diff([0 uniqueIndex])); newVals = cellfun(@nansum,valCell,'UniformOutput',false); 到呼叫[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mat2cell.html"]MAT2CELL[/URL]基团具有相同的开始日期一起放入一个单元阵列[B]valCell[/B]的细胞[B]瓦尔斯[/B]的行。可变[B]NEWVALS[/B]将长度[B]numel(uniqueStarts),[/B]其中每个单元将含有关于[B]valCell[/B]的对应的小区执行[B]nansum[/B]的结果的单元阵列。 [B]前循环解决方案:[/B] [Y,M] = datevec(allDts); monthStart = datenum(Y,M,1); % Start date of each month [monthStart,sortIndex] = sort(monthStart); % Sort the start dates [uniqueStarts,uniqueIndex] = unique(monthStart); % Get unique start dates vals = vals(sortIndex,:); % Sort the values according to start date nMonths = numel(uniqueStarts); uniqueIndex = [0 uniqueIndex]; newVals = nan(nMonths,size(vals,2)); % Preallocate for iMonth = 1:nMonths, index = (uniqueIndex(iMonth)+1):uniqueIndex(iMonth+1); newVals(iMonth,:) = nansum(vals(index,:)); end [url=https://stackoverflow.com/questions/863113]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 23:27。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.