Row subtraction in matrix
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
James Tursa
on 23 May 2018
Edited: James Tursa
on 23 May 2018
Just negate the usual diff() result. E.g.,
x = your matrix
result = -diff(x);
6 Comments
Stephen23
on 24 May 2018
Edited: Stephen23
on 24 May 2018
"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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!