Calculate difference between successive vector values.

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 日

19 投票

A=[1,2,3,3,0]
B=diff(A)

5 件のコメント

Jan
Jan 2012 年 8 月 21 日
編集済み: Jan 2012 年 8 月 21 日
This is much simpler and faster than A(2:end) - A(1:end-1). +1
William Summers
William Summers 2012 年 8 月 21 日
Brilliant, thank you!
Chloe
Chloe 2023 年 1 月 5 日
Is there any way to solve this problem using colon operator? Thanks!
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)
Dav
Dav 2023 年 8 月 10 日
Was looking for a function that did this, thanks!!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by