Trying to learn griddata function.
Mostrar comentarios más antiguos
Hi I'm triyng to learn the griddata command but I always come face to facve with NaN values in the griddata function. Can anyone explain why please?
clc;clear;
x1= rand(752,1);
x2= rand(752,1);
y1= rand(752,1);
[xx,yy]=meshgrid(x1,x2);
z= griddata(x1,x2,y1,xx,yy);
contourf(xx,yy,z);
Respuesta aceptada
Más respuestas (1)
John D'Errico
el 28 de Dic. de 2020
You have two (actually, three) problems here.
First, GRIDDATA is a interpolation tool. What does interpolation mean? It means it reproduces the original data point values, with NO error. But what happen if you have replicate data pooints?
That is, suppose you have the same, duplicated (x,y) pair, but with a different z value? How would an interpolation produce the SAME value if you give it that location to interpolate? Which value should it return in that event?
The solution that GRIDDATA uses (absolutely correct, IMHO) is to first replace any rreplicates with a SINGLE point at that location. Then average the z values at that location. What did GRIDDATA tell you it did?
Second, you say that you get NaNs in your output. This is also completely expected. tools like GRIDDATAQ use a triangulation of the data. That triangulation extends out to the convex hull of the data, but not beyond. For example...
x1= rand(75,1);
x2= rand(75,1);
T = delaunay(x1,x2);
trimesh(T,x1,x2);
hold on
plot(x1,x2,'o')
As you can see, the data points form an irregular potatoe in the (x,y) plane. But when you generate a grid of points, there will be many combinations that fall outside of that convex hull. As such, those points will be extrapolated. Any extrapolated point will yield a NaN from griddata as the interpolated value.
Finally, when you do this:
[xx,yy]=meshgrid(x1,x2);
this does NOT form any kind of useful grid in the (x,y) plane. Since x1 and y1 are scattered points in no special order, then meshgrid produces a result that looks the same way. Instead, you might have done this:
[xx,yy]=meshgrid(linspace(min(x1),max(x1),25),linspace(min(x2),max(x2),25));
plot(xx,yy,'k.')

I used a coarse grid there and only few points to make it clear what has heppened. The black dots show the grid. Any black dot that falls outside of the convex hull of the data will generate a NaN by griddata for the interpolated value.
1 comentario
Emre Can
el 28 de Dic. de 2020
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

