Linear Extrapolation with interp2

80 ビュー (過去 30 日間)
Michael Boutros
Michael Boutros 2020 年 5 月 16 日
編集済み: KD 2021 年 3 月 26 日
Matlab's griddedInterpolant allows for linear interpolation with linear extrapolation in 2D space, i.e.,
griddedInterpolant(X,Y,V,'linear','linear');
However, it seems to be the case that interp2, which I believe uses griddedInterpolant under-the-hood, doesn't allow for this combination. If "linear" is selected as the interpolation method, then the extrapolation must be a scalar. Is there a way to use linear-linear inter-extrapolation with interp2?
  4 件のコメント
John D'Errico
John D'Errico 2020 年 5 月 16 日
My question is still valid for Michael to answer as to why. Regardless, you can't force interp2 to do what it is not written to do. Anyway, IF the interp2 call is just going to get sent into griddedInterpolant anyway, then the GPU call would still seem to be a problem, since then interp2 is just a wrapper.
Matt J
Matt J 2020 年 5 月 16 日
I am curious of Michael's actual motives as well. Irrespectively, though, it is a frustrating problem for gpuArray users... I don't believe the gpuArray version of interp2 is a wrapper for anything. If a GPU version of griddedInterpolant existed, it would make sense for the Parallel Computing Toolbox developers to make it available to us.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2020 年 5 月 16 日
編集済み: Matt J 2020 年 5 月 16 日
The only remedy I can think of is to pre-pad the edges of the data V with linearly extrapolated values at distant X, Y. That way, you aren't doing any extrapolation anymore. All of your query points xq,yq will fall within the boundaries of the padded data, assuming you make the padded, X,Y distant enough to enclose them.
[V,x]=linearpad(V,x);
[V,x]=linearpad(fliplr(V),flip(x)); V=fliplr(V);
V=V.';
[V,y]=linearpad(V,y);
[V,y]=linearpad(fliplr(V),flip(y)); V=fliplr(V);
V=V.';
Vq=interp2(x,y,V,xq,yq,'linear');
function [D,z]=linearpad(D0,z0)
factor=1e6;
dz=z0(end)-z0(end-1);
dD=D(:,end)-D(:,end-1);
z=z0;
z(end+1)=z(end)+factor*dz;
D=D0;
D(:,end+1)=D(:,end) + factor*dD;
end
  1 件のコメント
KD
KD 2021 年 3 月 26 日
編集済み: KD 2021 年 3 月 26 日
FYI this almost works but there are mistakes. There should be no fliplr after applying linearpad the second time in each direction, and in the function it should be D=D0(:,end)-D0(:,end-1); (not D=D...)
Otherwise works great - thanks!
This is my modified version:
[V,x]=linearpad(V,x);
[V,x]=linearpad(fliplr(V),flip(x));
V=V.';
[V,y]=linearpad(V,y);
[V,y]=linearpad(fliplr(V),flip(y));
V=V.';
Vq=interp2(x,y,V,xq,yq,'linear');
function [D,z]=linearpad(D0,z0)
factor=1e6;
dz=z0(end)-z0(end-1);
dD=D0(:,end)-D0(:,end-1);
z=z0;
z(end+1)=z(end)+factor*dz;
D=D0;
D(:,end+1)=D(:,end) + factor*dD;
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by