Interpolation and missing values

4 Ansichten (letzte 30 Tage)
Chad Greene
Chad Greene am 21 Feb. 2014
Kommentiert: Chad Greene am 11 Mär. 2014
I have a series of data given by an x array and a corresponding y array. The arrays are equal in size. The x array is approximately equally spaced, but it has a few small gaps, and some very large gaps. I would like to interpolate to get numeric values in the small gaps, but I'd like to replace the big gaps with NaNs.
Here is an example of some small gaps, and one big gap. I'd like to replace the obviously incorrect interpolation with NaNs. How can I do this?
% create some data:
x = 0:.02:15;
y = sin(x);
% create some holes in the data
x([3 25 32:33 200:280 410:415]) = [];
y([3 25 32:33 200:280 410:415]) = [];
plot(x,y,'ko'); hold on
% the interpolation I'd like to improve:
xi = 0:.015:15;
yi = interp1(x,y,xi,'cubic');
plot(xi,yi,'b.')
  3 Kommentare
Chad Greene
Chad Greene am 21 Feb. 2014
Here is a loop that almost solves the problem. I'd rather avoid loops, but the biggest problem with this solution is the little tails that persist:
x = 0:.02:15;
y = sin(x);
x([3 25 32:33 200:280 410:415]) = [];
y([3 25 32:33 200:280 410:415]) = [];
plot(x,y,'ko'); hold on
xi = 0:.015:15;
yi = interp1(x,y,xi,'cubic');
for n = 1:length(xi)
if min(abs(xi(n)-x))>.1
yi(n) = NaN;
end
end
plot(xi,yi,'b.')
Above, a threshold is set to set to change any values more than 0.1 x units away from an x data point to NaN.
Chad Greene
Chad Greene am 21 Feb. 2014
I do have the Image Processing Toolbox, and I'm reading the documentation right now. Application of bwareaopen and regionprops is not immediately apparent.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Paul
Paul am 21 Feb. 2014
x_gap = x(2:end)-x(1:end-1);
ind=find(x_gap>0.025);
ind_int=[];
for j=1:numel(ind)
ind_int = [ind_int,find((xi>x(ind(j)) & xi<x(ind(j)+1)))];
end
yi(ind_int)=NaN;
  4 Kommentare
Paul
Paul am 23 Feb. 2014
add this:
yi = interp1(x,y,xi,'cubic');
after xi = 0:.015:15; and then it works.
Chad Greene
Chad Greene am 11 Mär. 2014
I turned Paul's solution into a function: http://www.mathworks.com/matlabcentral/fileexchange/45842. Thank you both for your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by