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 Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

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