|
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)?
|