How can I add the average of two consecutive numbers?

1 visualización (últimos 30 días)
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

Respuesta aceptada

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

Más respuestas (2)

Andrei Bobrov
Andrei Bobrov el 26 de Nov. de 2013
Editada: Andrei Bobrov el 26 de Nov. de 2013
y = randi(10,5,1);
Area = sum(conv2(y,[1;1]/2,'valid'));
  3 comentarios
Andrei Bobrov
Andrei Bobrov el 26 de Nov. de 2013
What should be correctly?
Marcela Ruiz de Chávez
Marcela Ruiz de Chávez el 26 de Nov. de 2013
I solved it. Thank you!!

Iniciar sesión para comentar.


Chibuzo Nnonyelu
Chibuzo Nnonyelu el 27 de Jun. de 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)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by