- If you leave the gaps in the original data, then those 0's will affect the interpolation results at neighboring sites, so they need to be discarded for the purposes of interpolation.
- If you don't request values on the full grid from griddata, adding the gaps back in afterward is harder.
Use griddata ignoring gaps in data
48 views (last 30 days)
I am using griddata on a scattered datatset (Data) to 'map' it onto a regular grid. The data is 2D and of the form: Data.x, Data.y, Data.z. The original data has some 'gaps'. All Data.x and Data.y values are valid with the 'gaps' being identified by Data.z = 0, and the remaining Data.z values are valid. Using griddata seems to still use these 'gap' data points and produces an output where the gaps are interpolated across using the surrounding 'valid' values. My code is as follows:
Data.Zgridded = griddata(Data.x, Data.y, Data.z, xgrid, ygrid);
where xgrid and ygrid are created using meshgrid.
My objective is to interpolate the scattered data onto a regular grid but omitting the 'gaps' from the interpolation (or assigning the gaps to NaNs or otherwise). Masking the data after griddata does not produce the correct result as the 'gap' data points are currently being incorrectly included in the first instance.
Any thoughts on how to tackle this are greatly appreciated.
Josh Meyer on 27 Oct 2017
To interpolate without the gaps you need to remove them from the data before interpolating, request values on the full grid (including gap sites), then add the gaps back in.
idx = (z ~= 0); %logical mask to keep nonzeros in z
x_adj = x(idx);
y_adj = y(idx);
z_adj = z(idx);
% interpolate on the full grid (x,y) using only the adjusted data
z_gridded = griddata(x_adj,y_adj,z_adj,x,y);
% use the mask again to restore the gaps.
% z_gridded then includes interpolated data + gaps over the full grid (x,y)
z_gridded(~idx) = 0;