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