how to create a vector with the different sum of two elements of another vector?

조회 수: 1 (최근 30일)
v = [1, 2, 3, 4, 6, 8];
l = length(v)
for i = 1 : 2 : l (?)
som = sum + v(i);
end
I don't know how to increse 'i' to create the sum of the two elements of the vector, the solution will be: som = [3, 7, 14]

채택된 답변

Stephen23
Stephen23 2019년 3월 17일
편집: Stephen23 2019년 3월 18일
No need to use a loop, some simple indexing works perfectly:
>> v = [1,2,3,4,6,8];
>> v(1:2:end)+v(2:2:end)
ans =
3 7 14
or generalized slightly:
>> sum(reshape(v,2,[]),1)
ans =
3 7 14
  댓글 수: 2
Enrica Brunetti
Enrica Brunetti 2019년 3월 18일
Thank you.
And then if I want to create another vector with the average of two different elements of the same first vector, is there another general function?
The solution will be a = [ 5, 7]

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Digital Filter Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by