How can I add the average of two consecutive numbers?

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 日

0 投票

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 日

0 投票

y = randi(10,5,1);
Area = sum(conv2(y,[1;1]/2,'valid'));

3 件のコメント

Marcela Ruiz de Chávez
Marcela Ruiz de Chávez 2013 年 11 月 26 日
It doesn't give me the right answer. I have this:
x0=1; x14=15; xr=x0:1:x14; y=A*xr.^3+B*xr.^2+C*xr+D; y_m=randi(10,15,1); Area=sum(conv2(y_m,[1;1]/2,'valid'))
Andrei Bobrov
Andrei Bobrov 2013 年 11 月 26 日
What should be correctly?
Marcela Ruiz de Chávez
Marcela Ruiz de Chávez 2013 年 11 月 26 日
I solved it. Thank you!!

サインインしてコメントする。

Chibuzo Nnonyelu
Chibuzo Nnonyelu 2018 年 6 月 27 日

0 投票

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)

カテゴリ

ヘルプ センター および File ExchangeMathematics and Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by