calculation of successive diffrence

2 ビュー (過去 30 日間)
Rica
Rica 2014 年 2 月 27 日
回答済み: Anuj 2014 年 2 月 27 日
Hi all!
I have a problem with this : I have an array:
% A=[a1 a2 a3 a4 ......an]
i want to calculate succesivelly the diffrenece:
% B=[a2-a1 a3-a2 a4-a3........] but with this condition:
if for exemple a(i)-a(i-1)==2, the diffrence a(i+1)-a(i-1) schould be calculated and the diffrence a(i)-a(i-1)==2 should be eleminated from the Array B.
i wish you could help , Thank you
  1 件のコメント
the cyclist
the cyclist 2014 年 2 月 27 日
編集済み: the cyclist 2014 年 2 月 27 日
Often in these cases, a small (but complete) example helps to illustrate what you mean. For example, what should the output be if
A = [2 3 5 7 11 13 17 19]
?
The way I am interpreting your question must be wrong, because I would calculate every difference, but then remove every difference from B, leaving an empty matrix.

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 2 月 27 日
B = diff(A);
B1 = conv(A,[1 0 -1],'valid');
idx = find(B == 2);
ii = idx <= numel(B1);
i2 = idx(ii);
B(i2) = B1(i2);

その他の回答 (1 件)

Anuj
Anuj 2014 年 2 月 27 日
B = diff(A);
B(find((B==2))=B(find(B==2)+1)+2

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by