Subtracting the next element on a vector? Basic

If I have a vector with n elements, and want a new vector with the subtraction of the first by the second element, and continues...
For exemple: A = [20, 8, 4, 1] turns into B = [(20-8),(8-4),(4,1)]
I started something with
n = length(A);
for k = 1:n
....
but nothing I tried worked them.
Thanks.

 Respuesta aceptada

Jiro Doke
Jiro Doke el 9 de Feb. de 2011
A = [20, 8, 4, 1];
B = -diff(A);

2 comentarios

Renato de Sá
Renato de Sá el 9 de Feb. de 2011
Thank you, sir.
Steven Lord
Steven Lord el 14 de En. de 2021
Jayaram ram flagged this as "its working good sir...."
The intended purpose of flags is to alert the editors and/or the admins about spam or other content that's not appropriate for Answers. To comment on the solution please use a comment like I'm doing here. Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 9 de Feb. de 2011

Comentada:

el 14 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by