How do I create a vector of the average of consecutive elements of another vector (without using a loop)?

조회 수: 16 (최근 30일)
Say, I have a vector A=[2 4 6 8 10]. Is there any way I can create a vector of the average of consecutive elements of A, i.e. B=[3 5 7 9] without using a loop? Is there any matlab function to do that? Thanks in advance.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 10일
편집: Azzi Abdelmalek 2013년 10월 10일
A=[2 4 6 8 10]
B=mean([A(1:end-1);A(2:end)])

추가 답변 (2개)

Jan
Jan 2013년 10월 10일
편집: Jan 2013년 10월 10일
This is simpler and occupies less temporary memory than Azzi's approach:
A = [2 4 6 8 10];
B = 0.5 * (A(1:end-1) + A(2:end))

Matthew Crema
Matthew Crema 2013년 10월 10일
One way:
A=[2 4 6 8 10];
foo = conv([1 1], A)/2;
B = foo(2:end-1)
B =
3 5 7 9
-Matt
  댓글 수: 5
Jan
Jan 2013년 10월 11일
The speed argument might change, when the data exceed a certain size.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by