Trying to learn griddata function.

14 visualizaciones (últimos 30 días)
Emre Can
Emre Can el 27 de Dic. de 2020
Comentada: Cris LaPierre el 28 de Dic. de 2020
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

Cris LaPierre
Cris LaPierre el 28 de Dic. de 2020
Editada: Cris LaPierre el 28 de Dic. de 2020
Your x1, x2 and xx, yy inputs do need to be unique. Since you create the vectors using rand, every once in a while you may get duplicate values, resulting in that error message.
I think your requested grid query points should be sorted.
data = rand(752,3);
x1= data(:,1);
x2= data(:,2);
y1= data(:,3);
% sorted x1, x2 inputs
[xx,yy]=meshgrid(sort(x1),sort(x2));
z= griddata(x1,x2,y1,xx,yy);
contourf(xx,yy,z);
Even here, the corners still have NaN values (more visible in a heatmap). This might be explained by this line in the documentation: "The specified query points must lie inside the convex hull of the sample data points. griddata returns NaN for query points outside of the convex hull."
heatmap(z,'GridVisible',false)
I suspect this is an artifact of using random numbers. You may get better results when using actual data.
  2 comentarios
Emre Can
Emre Can el 28 de Dic. de 2020
Thanks for your help sir. I understand it. I need to work with organized data when I using meshgrid and yes you are right it given more consistent results with my original data. It solved my problem.
It gives a warning but it contours my data correctly.
Warning: Duplicate data points have been detected and removed -
corresponding values have been averaged.
> In griddata>useScatteredInterp (line 181)
In griddata (line 122)
Cris LaPierre
Cris LaPierre el 28 de Dic. de 2020
This warning was explained in John D'Errico's reply.

Iniciar sesión para comentar.

Más respuestas (1)

John D'Errico
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
Emre Can el 28 de Dic. de 2020
Thanks for your answer sir. I read it and I understand logic of command.

Iniciar sesión para comentar.

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by