在matlab中使用interp3

时间:2014-08-01 09:42:56

标签: matlab octave

我试图了解如何使用3D matlab插值函数interp3。我尝试了不同的选项,但它一直给出错误。我有3个数组,其中包含V的坐标:

X = ndgrid(-246.4529 :2.5: -246.4529 + 100*2.5 - 1);
Y = ndgrid(-45.6577  :2.5: -45.6577  + 213*2.5 - 1);
Z = ndgrid(-211.00   :2.5: -211.00   + 140*2.5 - 1);

和其他3个数组,其中包含我想要插入的坐标:

Xp = ndgrid(-255.21 :5: -255.21 + 50*5  - 1);
Yp = ndgrid(-50.66  :5: -50.66  + 108*5 - 1);
Zp = ndgrid(-215.00 :5: -215.00 + 86*5  - 1);

然后我得到V这是一个样本值向量,指定为一个定义样本点值的向量。

 npoints = 100*213*140;
 for n=1:npoints 
     i = rem(n-1,dims(1)) + 1;
     P(n,1) = X(i);
     j = rem(fix((n-i)/dims(1)), dims(2)) + 1;
     P(n,2) = Y(j);
     k = ceil(n/(dims(1)*dims(2)));
     P(n,3) = Z(k);
    % store the sample 
     V(n,1) = mat(i,j,k);
 end

现在我想插入V以获取Vp

Vq = interp3(X,Y,Z,V,Xp,Yp,Zp)

但我有:

Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given values.

Error in interp3 (line 130)
        F = griddedInterpolant({X, Y, Z}, V, method,extrap);

关于如何将interp3用于我的数据的任何想法?

1 个答案:

答案 0 :(得分:2)

Interp3适用于meshgrid而不是ndgrid ...

Personnaly,因为当我试图理解带有meshgrid(和它返回的数组的大小)的网格向量的混合方向时,我总是很困惑**,我更喜欢使用有效的interpn来自ndgrid

% Input grid
x = (-246.4529 :2.5: -246.4529 + 100*2.5 - 1);
y = -45.6577  :2.5: -45.6577  + 213*2.5 - 1;
z = -211.00   :2.5: -211.00   + 140*2.5 - 1;

% Output grid
xp = -255.21 :5: -255.21 + 50*5  - 1;
yp = -50.66  :5: -50.66  + 108*5 - 1;
zp = -215.00 :5: -215.00 + 86*5  - 1;

% Data
V = randn(length(x),length(y),length(z));

然后使用interpnndgrid

% Interpolation
[X, Y, Z] = ndgrid(x, y, z);
[Xp, Yp, Zp] = ndgrid(xp, yp, zp);
Vq = interpn(X,Y,Z, V, Xp,Yp,Zp);

**:我认为meshgrid总是颠倒两个第一维