考虑以下矩阵,其中第一列是索引,第二列是值,第三列是累积和,一旦索引发生变化就会重置:
1 1 1 % 1
1 2 3 % 1+2
1 3 6 % 3+3
2 4 4 % 4
2 5 9 % 4+5
3 6 6 % 6
3 7 13 % 6+7
3 8 21 % 13+8
3 9 30 % 21+9
4 10 10 % 10
4 11 21 % 10+11
如何让第三列避免循环?
我尝试以下方法:
A = [1 1;... % Input
1 2;...
1 3;...
2 4;...
2 5;...
3 6;...
3 7;...
3 8;...
3 9;...
4 10;...
4 11];
CS = cumsum(A(:,2)); % cumulative sum over the second column
I = [diff(data(:,1));0]; % indicate the row before the index (the first column)
% changes
offset=CS.*I; % extract the last value of cumulative sum for a given
% index
offset(end)=[]; offset=[0; offset] %roll offset 1 step forward
[A, CS, offset]
结果是:
ans =
1 1 1 0
1 2 3 0
1 3 6 0
2 4 10 6
2 5 15 0
3 6 21 15
3 7 28 0
3 8 36 0
3 9 45 0
4 10 55 45
4 11 66 0
所以如果有一个简单的方法将上面矩阵的第四列转换成
,问题就会解决了。O =
0
0
0
6
6
15
15
15
15
45
45
因为CS-O提供了所需的输出。
我很感激任何建议。
答案 0 :(得分:6)
cumsum
和diff
的方法,因此可能对性能有好处 -
%// cumsum values for the entire column-2
cumsum_vals = cumsum(A(:,2));
%// diff for column-1
diffA1 = diff(A(:,1));
%// Cumsum after each index
cumsum_after_each_idx = cumsum_vals([diffA1 ;0]~=0);
%// Get cumsum for each "group" and place each of its elements at the right place
%// to be subtracted from cumsum_vals for getting the final output
diffA1(diffA1~=0) = [cumsum_after_each_idx(1) ; diff(cumsum_after_each_idx)];
out = cumsum_vals-[0;cumsum(diffA1)];
如果你关心性能,这里有一些基于accumarray
的其他解决方案的基准。
基准代码(删除了注释的紧凑性) -
A = .. Same as in the question
num_runs = 100000; %// number of runs
disp('---------------------- With cumsum and diff')
tic
for k1=1:num_runs
cumsum_vals = cumsum(A(:,2));
diffA1 = diff(A(:,1));
cumsum_after_each_idx = cumsum_vals([diffA1 ;0]~=0);
diffA1(diffA1~=0) = [cumsum_after_each_idx(1) ; diff(cumsum_after_each_idx)];
out = cumsum_vals-[0;cumsum(diffA1)];
end
toc,clear cumsum_vals diffA1 cumsum_after_each_idx out
disp('---------------------- With accumarray - version 1')
tic
for k1=1:num_runs
result = accumarray(A(:,1), A(:,2), [], @(x) {cumsum(x)});
result = vertcat(result{:});
end
toc, clear result
disp('--- With accumarray - version 2 (assuming consecutive indices only)')
tic
for k1=1:num_runs
last = find(diff(A(:,1)))+1; %// index of last occurrence of each index value
result = A(:,2); %// this will be cumsum'd, after correcting for partial sums
correction = accumarray(A(:,1), A(:,2)); %// correction to be applied for cumsum
result(last) = result(last)-correction(1:end-1); %// apply correction
result = cumsum(result); %// compute result
end
toc, clear last result correction
disp('--- With accumarray - version 2 ( general case)')
tic
for k1=1:num_runs
last = find(diff(A(:,1)))+1; %// index of last occurrence of each index value
result = A(:,2); %// this will be cumsum'd, after correcting for partial sums
correction = accumarray(A(:,1), A(:,2), [], @sum, NaN); %// correction
correction = correction(~isnan(correction)); %// remove unused values
result(last) = result(last)-correction(1:end-1); %// apply correction
result = cumsum(result);
end
toc
结果 -
---------------------- With cumsum and diff
Elapsed time is 1.688460 seconds.
---------------------- With accumarray - version 1
Elapsed time is 28.630823 seconds.
--- With accumarray - version 2 (assuming consecutive indices only)
Elapsed time is 2.416905 seconds.
--- With accumarray - version 2 ( general case)
Elapsed time is 4.839310 seconds.
答案 1 :(得分:5)
将accumarray
与自定义功能结合使用:
result = accumarray(A(:,1), A(:,2), [], @(x) {cumsum(x)});
result = vertcat(result{:});
无论索引更改是否为1步(如您的示例所示),都可以正常工作。
以下方法更快,因为它避免了细胞。在his answer中查看@Divakar的优秀基准测试(并查看他的解决方案,这是最快的):
如果索引更改始终对应于增加1(如示例所示):
last = find(diff(A(:,1)))+1; %// index of last occurrence of each index value
result = A(:,2); %// this will be cumsum'd, after correcting for partial sums
correction = accumarray(A(:,1), A(:,2)); %// correction to be applied for cumsum
result(last) = result(last)-correction(1:end-1); %// apply correction
result = cumsum(result); %// compute result
如果索引值的变化可以超过1(即可能有"跳过"值):这需要一个小的修改,这会稍微减慢速度。
last = find(diff(A(:,1)))+1; %// index of last occurrence of each index value
result = A(:,2); %// this will be cumsum'd, after correcting for partial sums
correction = accumarray(A(:,1), A(:,2), [], @sum, NaN); %// correction
correction = correction(~isnan(correction)); %// remove unused values
result(last) = result(last)-correction(1:end-1); %// apply correction
result = cumsum(result);
答案 2 :(得分:2)
你的策略实际上就是我所做的。你的最后一步可以通过这种方式实现:(但请记住,你的方法假定连续的索引。你当然可以通过offset=[0; CS(1:end-1).*(diff(A(:,1))~=0)];
改变它,但是仍然需要排序索引。)
I = find(offset);
idxLastI = cumsum(offset~=0);
hasLastI = idxLastI~=0; %// For the zeros at the beginning
%// Combine the above to the output
O = zeros(size(offset));
O(hasLastI) = offset(I(idxLastI(hasLastI)));
out = CS-O;
这应与 Divakar 的cumsum
- diff
方法相媲美。