필터 지우기
필터 지우기

counting n subsequent element of an array

조회 수: 4 (최근 30일)
Kaushik
Kaushik 2013년 1월 10일
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];

채택된 답변

Matt J
Matt J 2013년 1월 10일
b = conv(a,[1 1 1],'valid')

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 10일
편집: Azzi Abdelmalek 2013년 1월 10일
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)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by