create a new column of data showing (n+1)-n of old column

1 vue (au cours des 30 derniers jours)
Joseph Tirado
Joseph Tirado le 16 Jan 2022
Commenté : Joseph Tirado le 17 Jan 2022
I need to find the amount each data point increases by for each point in a clumn of data. I want to create a new column that calcuates the next point subtracted by the point right before it. if the colmn was labeled "x" it would function something like this: x(n+1)-x(n). Can anyone help?

Réponse acceptée

Cris LaPierre
Cris LaPierre le 16 Jan 2022
This is what the diff function does.
  • If X is a vector of length m, then Y = diff(X) returns a vector of length m-1. The elements of Y are the differences between adjacent elements of X.Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]

Plus de réponses (1)

Image Analyst
Image Analyst le 16 Jan 2022
Modifié(e) : Image Analyst le 16 Jan 2022
Do you mean like using the diff() function?
m = randi(9, 5, 7)
m = 5×7
1 6 6 9 2 4 1 8 8 4 8 3 7 2 3 6 7 1 7 7 6 8 1 8 3 9 1 8 5 3 8 1 2 7 9
dHoriz = diff(m, 1, 2)
dHoriz = 5×6
5 0 3 -7 2 -3 0 -4 4 -5 4 -5 3 1 -6 6 0 -1 -7 7 -5 6 -8 7 -2 5 -7 1 5 2
dVert = diff(m, 1, 1)
dVert = 4×7
7 2 -2 -1 1 3 1 -5 -2 3 -7 4 0 4 5 -5 1 2 2 -6 2 -3 2 0 -2 -7 6 1

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by