Row sums by vector indices without for loop?

Best illustrated by example
M=rand(5,10);
s=[1 3; 5 8; 9 10];
for i=1:size(s,1)
sm(:,i)=sum(M(:,s(i,1):s(i,2)),2);
end
Is there a one-liner or short-cut for the for loop? I do not know how large M or s is ahead of time, or their values. Above code shown only for illustration of problem trying to solve.
Thanks!
edit: Corrected typo of 'm' in for-loop to 'M'

 채택된 답변

Walter Roberson
Walter Roberson 2011년 2월 7일

0 개 추천

If your accuracy requirements are not high and your values are not extreme, you can use cumsum(), after which it becomes the difference of the cumsum indexed at two positions -- a transformation that gets around the problem that your indices do not always designate the same number of elements to be summed per row.
If I recall collectly, Bruno, and Matt Fig, both have posted non-cumsum() versions of this task in the past (it has come up before in cssm discussions.)

댓글 수: 2

Oleg Komarov
Oleg Komarov 2011년 2월 7일
There's a submission by Bruno, mcolon, which will allow you to create the indexes and use accumarray avoiding the multiple call to sum.
Mike Sheppard
Mike Sheppard 2011년 2월 8일
I like the cumsum solution for its simplicity.
cm=cumsum([zeros(size(M,1),1) M],2);
sm2=cm(:,s(:,2)+1)-cm(:,s(:,1))
Just had to add a column of zeros; but I like the calculus F(b)-F(a)intuitive solution. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2011년 2월 7일

2 개 추천

Yes, to be more specific my mcolon on FEX
can be used like this:
% Data
M = ceil(10*rand(5,10))
s = [1 3; 5 8; 9 10];
% Engine
[a i] = mcolon(s(:,1),s(:,2));
[r c] = ndgrid(1:size(M,1),i);
Ma = M(:,a);
sm = accumarray([r(:) c(:)], Ma(:))
Bruno

댓글 수: 2

Jan
Jan 2011년 2월 7일
Did you compare the speed with the OP's FOR loop method with adding a pre-allocation?
Bruno Luong
Bruno Luong 2011년 2월 8일
Following Jan's question: I make the following test and I get the 25% acceleration by using MCOLON
Time relative for-loop/mcol = 1.26013/1
% Code
ntest = 10;
t = zeros(2,ntest);
for n=1:ntest
s = ceil(10*rand(1,2*10000));
s = cumsum(s);
s = reshape(s, 2, [])';
M=rand(5,s(end));
% for loop Engine
tic
sm = zeros(size(M,1),size(s,1));
for i=1:size(s,1)
sm(:,i)=sum(M(:,s(i,1):s(i,2)),2);
end
t(1,n) = toc;
% mcolon Engine
tic
[a c] = mcolon(s(:,1),s(:,2));
[r c] = ndgrid(1:size(M,1),c);
Ma = M(:,a);
sm = accumarray([r(:) c(:)], Ma(:));
t(2,n) = toc;
end
t = min(t,[],2);
t = t/min(t);
fprintf('Time relative for-loop/mcol = %g/%g\n', t);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by