how can I correct errors by interpolation?

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by