functions handle for a summation over n elements

Hello!
I'm new to function handles and I find it difficult to solve the following problem. I would like to define a function
f=@(X) (X(1)-X(2))^2 + (X(2)-X(3))^2 + ... + (X(n-1)+X(n))^2
where X is a n-dimensional vector.
As n may be large, I will end up with a problem: Obviously I don't want to type n addents! Is there an easy way to overcome this problem?
Thanks!
Regards Fred

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 30 日

0 投票

f=@(x) sum(diff(x).^2)

1 件のコメント

Fred
Fred 2014 年 3 月 30 日
編集済み: Fred 2014 年 3 月 30 日
Wow, that was quick :D
Thank you!

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2014 年 3 月 30 日

編集済み:

2014 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by