How can I add the average of two consecutive numbers?

1 次查看(过去 30 天)
How can I put this in a simpler form?
Area=(y(1)+y(2)/2+(y(2)+y(3)/2+(y(3)+y(4)/2+(y(4)+y(5))/2

采纳的回答

David Sanchez
David Sanchez 2013-11-26
n = length(y);
Area = 0;
for k=1:(n-1)
Area = Area + (y(k) +y(k+1))/2;
end

更多回答(2 个)

Andrei Bobrov
Andrei Bobrov 2013-11-26
编辑:Andrei Bobrov 2013-11-26
y = randi(10,5,1);
Area = sum(conv2(y,[1;1]/2,'valid'));
  3 个评论

请先登录,再进行评论。


Chibuzo Nnonyelu
Chibuzo Nnonyelu 2018-6-27
By using indexing and simple maths (y1 + y2)/2 = (y2 - y1)/2 + y1 therefore, you can do this as
Area = diff(y)/2 + y(1, end - 1)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by