counting n subsequent element of an array
Show older comments
say i have an array a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
is there a easy way to add say n=3 elements at a time and form another array b. that is elements of b will be as follows:
b = [1+2+3, 2+3+4, 3+4+5, 4+5+6, 5+6+7,6+7+8,7+8+9, 8+9+10];
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 10 Jan 2013
Edited: Azzi Abdelmalek
on 10 Jan 2013
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
n=3
b=arrayfun(@(x) sum(a(x:x+n-1)),1:numel(a)-n+1)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!