How can I add the average of two consecutive numbers?
8 views (last 30 days)
Show older comments
Marcela Ruiz de Chávez
on 26 Nov 2013
Answered: Chibuzo Nnonyelu
on 27 Jun 2018
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
0 Comments
Accepted Answer
David Sanchez
on 26 Nov 2013
n = length(y);
Area = 0;
for k=1:(n-1)
Area = Area + (y(k) +y(k+1))/2;
end
0 Comments
More Answers (2)
Andrei Bobrov
on 26 Nov 2013
Edited: Andrei Bobrov
on 26 Nov 2013
y = randi(10,5,1);
Area = sum(conv2(y,[1;1]/2,'valid'));
3 Comments
Chibuzo Nnonyelu
on 27 Jun 2018
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)
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!