Row subtraction in matrix

1 次查看(过去 30 天)
Ian Loveless
Ian Loveless 2018-5-23
I am trying to subtract each row in a matrix from the row after it. The function diff does x_{i} - x_{i+1}, I need to do x_{i}-x_{i-1}. I cannot find anything that will tell me how to do this. I am sure it is something simple that I am just overlooking.
  3 个评论
Stephen23
Stephen23 2018-5-23
The diff help states that for a matrix it returns
[X(2,:)-X(1,:); X(3,:)-X(2,:); ... X(p,:)-X(p-1,:)]
which seems to match what you asked for.
Ian Loveless
Ian Loveless 2018-5-23
Then I mistyped. Is there a way I can do row{i}-row{i+1}?

请先登录,再进行评论。

回答(1 个)

James Tursa
James Tursa 2018-5-23
编辑:James Tursa 2018-5-23
Just negate the usual diff() result. E.g.,
x = your matrix
result = -diff(x);
  6 个评论
Stephen23
Stephen23 2018-5-24
编辑:Stephen23 2018-5-24
"It is the same as my diff() matrix, except each value has "-" in front of it."
That would be exactly correct then. The only difference between these
row(i)-row(i+1)
row(i)-row(i-1)
will be the sign. So if all were positive in the first case, then in the second case all would be negative.
Ian Loveless
Ian Loveless 2018-5-25
You are all correct, I apologize for my ignorance. I was looking at spacings instead of differences. Thank you for your help and patience.

请先登录,再进行评论。

类别

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

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by