One line code to alternate diff

1 回表示 (過去 30 日間)
Mate 2u
Mate 2u 2012 年 4 月 4 日
Hi everyone. I know that diff(x) produces the difference in x elements.
Now lets say I want to produce the difference of every 2 elements in x? Or even 3 elements.....rather than 1 (in the case of diff).
Could can body know how to do this?
Thanks

採用された回答

Titus Edelhofer
Titus Edelhofer 2012 年 4 月 4 日
Hi,
not sure, if I understood correctly, but does this what you need?
% some data
x = 1:10;
% n = 2 or 3 or ...
n = 2;
% build the difference ...
dx = x(n+1:end) - x(1:end-n)
This would work for vectors only, but generalization to matrices is of course straigt forward ...
Titus

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by