Calculate difference between successive vector values.

Hi all, Hopefully a straightforward question, but one that I'm struggling with. I have a vector and want to create a new vector whose values are equal to the difference between successive values from the previous vector. e.g A = [1,2,3,3,0] want B = [1,1,0,-3]
What is the simplest way of doing this? Many thanks in advance. Will

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 21 de Ag. de 2012
Editada: Azzi Abdelmalek el 21 de Ag. de 2012
A=[1,2,3,3,0]
B=diff(A)

5 comentarios

Jan
Jan el 21 de Ag. de 2012
Editada: Jan el 21 de Ag. de 2012
This is much simpler and faster than A(2:end) - A(1:end-1). +1
Brilliant, thank you!
Chloe
Chloe el 5 de En. de 2023
Is there any way to solve this problem using colon operator? Thanks!
The method given in Jan's comment above uses the colon operator:
A = [1,2,3,3,0];
B = A(2:end) - A(1:end-1)
Dav
Dav el 10 de Ag. de 2023
Was looking for a function that did this, thanks!!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 21 de Ag. de 2012

Comentada:

Dav
el 10 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by