Calculate difference between successive vector values.

169 次查看(过去 30 天)
Hi all, Hopefully a straightforward question, but one that I'm struggling with. I have a vector and want to create a new vector whose values are equal to the difference between successive values from the previous vector. e.g A = [1,2,3,3,0] want B = [1,1,0,-3]
What is the simplest way of doing this? Many thanks in advance. Will

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2012-8-21
编辑:Azzi Abdelmalek 2012-8-21
A=[1,2,3,3,0]
B=diff(A)
  5 个评论
Voss
Voss 2023-1-5
The method given in Jan's comment above uses the colon operator:
A = [1,2,3,3,0];
B = A(2:end) - A(1:end-1)

请先登录,再进行评论。

更多回答(0 个)

类别

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