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

1 次查看(过去 30 天)
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?

采纳的回答

Cris LaPierre
Cris LaPierre 2022-1-16
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)]

更多回答(1 个)

Image Analyst
Image Analyst 2022-1-16
编辑:Image Analyst 2022-1-16
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

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by