Alternatives to using diff(X,2)

Just a simple question:
We have an alternative for the opreation diff(A,1) given by A(2:end)-A(1:end-1), from an earlier post.
How to generalize it for diff(A,2)? for say A=[1 2 3 4 5 6]? to get [0 0 0 0].
Thanks.

 採用された回答

Bruno Luong
Bruno Luong 2018 年 12 月 8 日
編集済み: Bruno Luong 2018 年 12 月 8 日

2 投票

Just apply the same method twice
D1 = A(2:end)-A(1:end-1);
D2 = D1(2:end)-D1(1:end-1)
or
D2 = conv(A,[1 -2 1],'valid')

3 件のコメント

AtoZ
AtoZ 2018 年 12 月 8 日
Perfect, Thanks :)
AtoZ
AtoZ 2018 年 12 月 8 日
編集済み: AtoZ 2018 年 12 月 8 日
Does it also work for sym type inputs? the conv(..) thing gives error for sym type input while the D2 prescription works.
Bruno Luong
Bruno Luong 2018 年 12 月 8 日
No this is numerical difference method

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2014a

タグ

質問済み:

2018 年 12 月 8 日

コメント済み:

2018 年 12 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by