MATLAB Answers

## Interpolating 2D data including gaps (rawZ = 0) with GRIDDATA(), but without filling in the data gaps

Asked by Ivar Teeseling

### Ivar Teeseling (view profile)

on 16 Sep 2018
Latest activity Commented on by jonas

### jonas (view profile)

on 17 Sep 2018
Accepted Answer by jonas

### jonas (view profile)

Background:
• I am trying to project scattered 2D raw data (rawX, rawY, rawZ) onto a 2D grid (intX, intY) using GRIDDATA()
• The scattered 2D raw data has a data gap where no measurements have been made (rawZ = 0), as shown in the figure
• When I use GRIDDATA(), the data gap gets automatically filled in by the interpolation operation (see figure), but I want the data gap to exist in the interpolated data as well
• intX, intY are created using MESHGRID()
Tried Methods
• Setting rawX and/or rawY = NaN, but this does not help
• I do ignore the gapped data points (rawZ == 0) doing the interpolation as the zero values will locally distort the interpolated values
Question
• Is there method to directly have GRIDDATA() ignore the rawZ data gap when creating intZ?
Much appreciated!!
I have attached the code below:
% Remove data gaps for interpolation
mask = (rawZ==0);
rawX_adj = rawX(~mask);
rawY_adj = raw(~mask);
rawZ_adj = raw(~mask);
% Create the interpolation mesh grid (intX, intY)
intX = linspace(min(rawX_adj(:)), max(rawX_adj(:)), 100);
intY = linspace(min(rawY_adj(:)), max(rawY_adj(:)), 100);
[intX, intY] = meshgrid(intX, intY);
% Project the raw data onto the (intX, intY) grid
intZ = nangriddata(rawX_adj, rawY_adj, rawZ_adj, intX, intY);

#### 0 Comments

Sign in to comment.

## 1 Answer

Answer by jonas

on 16 Sep 2018
Edited by jonas

### jonas (view profile)

on 17 Sep 2018
Accepted Answer

The nature of your data is a bit unclear. You say scattered data but that the hole is associated with a value of zero. If you have coordinates for the hole, then Id just extract those, define a polygon around the hole using boundary and remove all points inside the polygon after interpolation using inpolygon .
Some untested code:
%%Find coordinates inside hole
xh=rawX((rawZ==0)):
yh=rawY((rawZ==0)):
%%Define bounding polygon
p=boundary(xh,yh);
%%Interpolate
[X,Y]=meshgrid(resx,resy);
Z=griddata(rawX,rawY,rawZ,X,Y)
%%Find and remove pts inside polygon and plot
in=inpolygon(X,Y,xh(p),yh(p))
Z(in)=NaN;
surf(X,Y,Z)
Thats simple. The following two solutions are for when the holes are defined by their lack of data:
Technically, there are gaps between every one of your data points and you want to remove large gaps. There is, to my knowledge, no option in griddata to define the "largest distance" over which it is allowed to interpolate. Here are however two options for you where you first interpolate and then remove the data where the holes were.
Method 1:
Create a grid for interpolation and find the closest distance between each point in your grid and a data point using dsearchn .
%%Make some data
x=rand(1,1e4)';
y=rand(1,1e4)';
z=x.*y;
xp=[0.4 0.6 0.6 0.4 0.4];
yp=[0.4 0.4 0.6 0.6 0.4];
in=inpolygon(x,y,xp,yp);
x(in)=[];
y(in)=[];
z(in)=[];
%%Plot data
subplot(1,3,1)
plot(x,y,'.')
%%Desired grid
[X,Y]=meshgrid(0:0.01:1,0:0.01:1);
%%Find holes
[a b]=dsearchn([x,y],[X(:),Y(:)])
subplot(1,3,2)
scatter(X(:),Y(:),[],b)
id=a(b>.02)