how can I correct errors by interpolation?

2 views (last 30 days)
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

Accepted Answer

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

More Answers (1)

dpb
dpb on 8 Apr 2014
>> 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 Comments
Tiberius
Tiberius on 8 Apr 2014
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 on 8 Apr 2014
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... :)

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!