フィルターのクリア

Diff but not when 0

2 ビュー (過去 30 日間)
Kambren Rowlan
Kambren Rowlan 2017 年 7 月 21 日
編集済み: dpb 2017 年 7 月 21 日
I need a way to do what diff does but ignore differences when the upper bound or lower bound is 0;
X = [1 1 2 0 5 0 13 21];
Y = diff(X)
Y =
0 1 -2 5 -5 13 8
X = [1 1 2 0 5 0 13 21];
Y = the_diff_I_Need(X)
Y =
0 1 0 0 0 0 8
Efficiency is important here.
Thanks

採用された回答

dpb
dpb 2017 年 7 月 21 日
編集済み: dpb 2017 年 7 月 21 日
X(X==0)=nan; % flag zero locations
d=diff(X); % compute difference w/ flagged
d(isnan(d))=0; % replace NaN in output if desired
For the sample given
>> Y=X;Y(Y==0)=nan;
>> d=diff(Y); d(isnan(d))=0
d =
0 1 0 0 0 0 8
>>

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by