Error in interp2: The grid vectors do not define a grid of points that match the given values

14 次查看(过去 30 天)
I have an map z of size [512,721] and two corresponding axes x and y with 512 and 721 elements respectively. I want to interpolate the map to 1024 by 1024 points and am using interp2 for this.
I generate new axes xi and yi as follows:
x = 1:512; y = 1:721;
nx = 512; ny = 721; ni = 1024;
xi = 1:nx/ni:ni; yi = 1:ny/ni:ny;
I know that xi and yi have only 1023 elements now put at the moment I am struggling with the error I get when running interp2. So here is a short minimal example:
z = rand(n1,n2); %original 2D-map
zi = interp2(x,y,z,xi,yi);
This throws the error:
Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given
values.
The same happens if I run the code with meshgrids instead of vectors:
[xm,ym] = meshgrid(x,y);
[xmi,ymi] = meshgrid(xi,yi);
zmi = interp2(xm,ym,z,xmi,ymi);
I also tried switching x and y (and xi and yi) in interp2, in which case the code runs without an error, however zmi is then a vector with 1023 elements:
zmiflipped = interp2(y,x,z,yi,xi);
all(size(zmiflipped) == [ 1,1023])
Can someone help me understand the error and explain to me how I can fix it? I am using MATLAB R2013a
  2 个评论
Torsten
Torsten 2015-7-23
x,y and z must be vectors of the same length (in your case 512*721). z(i) is the function value in point (x(i),y(i)) (i=1,...,512*721).
This is not the case in your example.
Best wishes
Torsten.
Till
Till 2015-7-23
Thanks for your reply Torsten.
Sorry if this is obvious but do you mean that i need to transpose xm and ym when I want to insert them into the interpolation with meshgrids? Because in this case I get the new error:
Data is in MESHGRID format, NDGRID format is required.
Convert your data as follows:
X = X'; Y = Y'; V = V'; F = griddedInterpolant(X,Y,V)
which to me seems to indicate that transposing would not solve this issue.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2015-7-23
x = 1:512;
y = 1:721;
nx = 512;
ny = 721;
ni = 1024;
xi = linspace(1,nx,ni);
yi = linspace(1,ny,ni);
[X,Y] = ndgrid(x,y);
z = rand(nx,ny);
F = griddedInterpolant(X,Y,z,'cubic');
[XI,YI] = ndgrid(xi,yi);
zi = F(XI,YI);
Plese read about griddedInterpolant.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by