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

1 ビュー (過去 30 日間)
Joseph Tirado
Joseph Tirado 2022 年 1 月 16 日
コメント済み: Joseph Tirado 2022 年 1 月 17 日
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 件のコメント
Joseph Tirado
Joseph Tirado 2022 年 1 月 17 日
Thanks for the help!

サインインしてコメントする。

その他の回答 (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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by