functions handle for a summation over n elements

3 visualizaciones (últimos 30 días)
Fred
Fred el 30 de Mzo. de 2014
Editada: Fred el 30 de Mzo. de 2014
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

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 30 de Mzo. de 2014
f=@(x) sum(diff(x).^2)
  1 comentario
Fred
Fred el 30 de Mzo. de 2014
Editada: Fred el 30 de Mzo. de 2014
Wow, that was quick :D
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by