Borrar filtros
Borrar filtros

Subtracting one row from a previous row in a single column of n long data

11 visualizaciones (últimos 30 días)
I have a single column of data that is n long and I want to be able to subtract the second row in that column by the first and have that pattern repeat. (third row subtracted by the second, fourth row subtracted by the thrid and so on).
Ex:
1
5
6
7
4
3
I would like to be able to have a code that does 5-1, 6-5, 7-6, 4-7, 4-3.
I am pretty new to MatLab so if I am using incorrect terminology or my question is confusing in any way please let me know.

Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de Feb. de 2022
A = randi(9, 8, 2) %some data to illustrate
A = 8×2
1 4 9 2 9 7 6 3 1 9 5 7 5 8 7 8
diff(A, [], 1) %subtract row by row
ans = 7×2
8 -2 0 5 -3 -4 -5 6 4 -2 0 1 2 0

Más respuestas (1)

Cris LaPierre
Cris LaPierre el 5 de Feb. de 2022
Use the diff function
a = [1
5
6
7
4
3];
diff(a)
ans = 5×1
4 1 1 -3 -1
  7 comentarios
Jacob Allen
Jacob Allen el 6 de Feb. de 2022
Okay, i'll give that a shot. Thanks for the explanation.
Walter Roberson
Walter Roberson el 10 de Feb. de 2022
It is common that gradient() should be used instead of diff()

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by