how can I correct errors by interpolation?
조회 수: 3 (최근 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
댓글 수: 0
채택된 답변
Walter Roberson
2014년 4월 8일
a(a < 0 | a > 4) = nan;
then you can use the File Exchange contribution inpaint_nan
댓글 수: 0
추가 답변 (1개)
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
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... :)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!