Use griddata ignoring gaps in data

조회 수: 36 (최근 30일)
Rob
Rob 2017년 10월 27일
댓글: Rob 2017년 10월 27일
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
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.
  • 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.
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;
  댓글 수: 1
Rob
Rob 2017년 10월 27일
Thank you, I got that to work.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by