MATLAB: How avoid interpolated values between two clouds of scattered data

interpolationscattered data

I have an extreme sparse set of lat-lon scattered data and I need to estimate values in a regular grid. I would like to avoid to have interpolated values when I am far from data (for a given threshold), and rather having NaN in this case. Is there any practical option for doing this?
Here is an example (messy!) code:
% a. dummy scattered data (two clouds);
x=[rand(1,11).*100, rand(1,11).*100+400];
y=[rand(1,11).*100, rand(1,11).*100+400];
z=rand(1,22).*100;
scatter(x,y,30,z); colorbar; % plot
% b. interpolation
F=TriScatteredInterp(x',y',z'); % interpolant (I know I should not use TriScatteredInterp anymore..)
[xi,yi]=meshgrid([0:10:500],[0:10:500]);% values for regular grid
zi=F(xi,yi);% extract on my regular grid
%
figure(), surface(xi,yi,zi); % plot interpolated value
What I want is that not only in the convex hull, but also if no data are found within a given distance are set to NaN. it is a sort of clustering, I guess… is there any easy option or interpolation trick to do this? Any help is appreciated! thank you.

Best Answer

There is no way to do it without some effort on your part. It is not terribly hard to solve though.
A scattered interpolant will work within the convex hull of the data. But if you have two distinct point clouds, then the hole in between the clouds will still be part of the convex hull. Interpolation may well yield garbage predictions there, but points inside the convex hull but outside of the clouds themselves are still inside as far as a convex hull is concerned. The software cannot magically know what you want.
I'd suggest identifying the two distinct clouds. If you have no simple scheme to do so, then a clustering tool may be needed, so perhaps a k-means tool. Build the two convex hulls separately. Then use a test to see if the target point is inside the convex hull of EITHER cloud. (So actually two different tests.) If so, then and only then will you use a tool like scatteredInterpolant to interpolate within the appropriate point cloud.