Thread Subject: Sum of elements in a vector

 Subject: Sum of elements in a vector From: Tiago Silva Date: 29 Jan, 2009 18:26:36 Message: 1 of 6 Hello everyone, I'm a bit stuck on the following: I have a vector with N elements (where N = a lot) and I want to obtain the sum of each set of M adjacent elements. Just as an example, with N=10 and M=3, I want to go from [1 2 3 4 5 6 7 8 9 10] to [6 9 12 15 18 21 24 27] My first thought was to replicate the original vector M times, shift each of rows of the resulting MxN matrix as necessary and then sum along the collumns, but I'm stuck since I can't seem to figure out a way shift each row by a different amount. I'm pretty sure there should be a way to solve this elegantly (and which probably doesn't have anything to with my idea), but I'm having problems figuring it out. Any ideas?
 Subject: Sum of elements in a vector From: Roger Stafford Date: 29 Jan, 2009 19:04:01 Message: 2 of 6 Tiago Silva wrote in message <27750925.1233253627451.JavaMail.jakarta@nitrogen.mathforum.org>... > ..... > I have a vector with N elements (where N = a lot) and I want to obtain the sum of each set of M adjacent elements. > ....... > Just as an example, with N=10 and M=3, I want to go from > > [1 2 3 4 5 6 7 8 9 10] > > to > > [6 9 12 15 18 21 24 27] > ....... Let x be the original vector and M the number of terms to be added. Then do  N = length(x);  y = sum(toeplitz(x(1:M),x(M:N)),1); y will be the desired result.   Of course if you want x to always be the integers 1:N there is a well known expression for the sums: (M+2*i-1)*M/2. Roger Stafford
 Subject: Sum of elements in a vector From: Roger Stafford Date: 29 Jan, 2009 19:24:02 Message: 3 of 6 "Roger Stafford" wrote in message ... > ...... > Of course if you want x to always be the integers 1:N there is a well known expression for the sums: (M+2*i-1)*M/2. > .....   I should have put that last in nice matlab form:  y = (M+1)*M/2:M:(M+2*N-5)*M/2 Roger Stafford
 Subject: Sum of elements in a vector From: Walter Roberson Date: 29 Jan, 2009 19:32:55 Message: 4 of 6 Tiago Silva wrote: > I have a vector with N elements (where N = a lot) and I want to obtain the sum of each > set of M adjacent elements. T = [0 cumsum(TheVector)]; SumEachM = T(M:end) - T(1:end-M+1); %I might have an off-by-one error here If, however, the values are such that the cummulative sum could involve significant loss of precision, then something like: sum(TheVector(cell2mat(bsxfun(@plus, 0:length(TheVector)-M, (1:M).', 'Uniform', 0))),2) (Sorry, it'd be a nuisance to log in through our firewall layers to cross-check that I have everything exactly right.) -- .signature note: I am now avoiding replying to unclear or ambiguous postings. Please review questions before posting them. Be specific. Use examples of what you mean, of what you don't mean. Specify boundary conditions, and data classes and value relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?
 Subject: Sum of elements in a vector From: us Date: 29 Jan, 2009 20:30:19 Message: 5 of 6 Tiago Silva > Just as an example, with N=10 and M=3, I want to go from > [1 2 3 4 5 6 7 8 9 10] > to > [6 9 12 15 18 21 24 27] one of the many solutions % the data      m=1:10;      n=3; % the engine      mf=filter(ones(1,n),1,m); % the result % - note: first two entries NOT removed...      disp([m;mf]); %{      1 2 3 4 5 6 7 8 9 10      1 3 6 9 12 15 18 21 24 27 %} us
 Subject: Sum of elements in a vector From: Tiago Silva Date: 30 Jan, 2009 08:45:49 Message: 6 of 6 Many thanks everyone. Now just need to figure out which one is faster :)

Everyone's Tags:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.