- 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
36 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Josh Meyer
2017-10-27
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.
For example:
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;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!