フィルターのクリア

how can I correct errors by interpolation?

3 ビュー (過去 30 日間)
Tiberius
Tiberius 2014 年 4 月 8 日
コメント済み: dpb 2014 年 4 月 8 日
Let's say I have this : a=[2 1 720 2.3 2.6 -40 -2 7 3] and I want to keep all the values in the interval (0,4) as they are and interpolate others either by mean (for one 'error') or by some sort of linspace. Thus, the matrix will become a=[2 1 1.65 2.3 2.6 2.7 2.8 2.9 3]. Thanks

採用された回答

Walter Roberson
Walter Roberson 2014 年 4 月 8 日
a(a < 0 | a > 4) = nan;
then you can use the File Exchange contribution inpaint_nan

その他の回答 (1 件)

dpb
dpb 2014 年 4 月 8 日
>> a=[2 1 720 2.3 2.6 -40 -2 7 3];
>> ix=find(~iswithin(a,0,4))
>> i1=iswithin(a,0,4);
>> interp1(find(i1),a(i1),ix)
ans =
1.6500 2.7000 2.8000 2.9000
>> b=a;b(ix)=ans
b =
2.0000 1.0000 1.6500 2.3000 2.6000 2.7000 2.8000 2.9000 3.0000
iswithin is my utility helper function--
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
that is simply "syntactic sugar" to put the ugly condition test out of sight.
You can obviously shorten the final by getting rid of some temporaries; just demonstrating the idea.
Unfortunate that you can't tell interp1 to ignore the values at the Xq locations even if present and have to make the selection w/o them to not just return the values in the a vector for them...that would save a step.
  2 件のコメント
Tiberius
Tiberius 2014 年 4 月 8 日
Thanks for the quick answers. The inpaint_nans function works wonderfully and I think the second solution works too. I tried several conditional statements myself before coming here. Thanks again.
dpb
dpb 2014 年 4 月 8 日
Walter's utility moves the explicit reduction into a lower level as my iswithin does excepting using NaN as the flag variable instead of selecting the subset. Same cat, different skin... :)

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by