Interpolate differently along different directions

2 ビュー (過去 30 日間)
Andrea Somma
Andrea Somma 2024 年 5 月 24 日
回答済み: Aneela 2024 年 6 月 5 日
I have a field (V) which is defined on a meshgrid X,Y,Z,V and a set of point on which I want the interpolated field, but I want it to be interpolated along x whith the nearest method (because of some conservation properties of the fields) and along z and y with the linear method is it possible? Can you provide a simple example. Thank you in advance
  2 件のコメント
Star Strider
Star Strider 2024 年 5 月 24 日
See if the interpn function will work to solve your problem. You apparently have a 4-D gridded matrix, and interpn may be appropriate for what I believe you want to do. See the documentation for examples.
Andrea Somma
Andrea Somma 2024 年 5 月 24 日
The grid is 3d V is the value of the field on each point, interpn does not permit to split the interpolation method along coordinates as far as I know

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

採用された回答

Aneela
Aneela 2024 年 6 月 5 日
Hi Andrea,
“interpn” allows for interpolating within an N-dimensional space but assumes the same type of interpolation across all dimensions.
It does not support using different interpolation methods for different dimensions in a single call.
However, it is possible to interpolate along x with “nearest” method and along y and z with “linear” method manually.
Here’s a workaround:
[X, Y, Z] = meshgrid(1:10, 1:10, 1:10); % Original grid coordinates
V = sin(X) + cos(Y) + sin(Z);
% Define target points
Xq = [3.5, 4.5, 5.5];
Yq = [2.5, 7.5, 4.5];
Zq = [1.5, 8.5, 5.5];
% Nearest interpolation along X
% For nearest neighbor along X, we find the closest X grid point for each query point
[~, idx] = min(abs(X(1,:,1) - Xq'), [], 2); % Find indices of nearest X grid points
Vq = zeros(size(Xq));
for i = 1:length(Xq)
% Extract a 2D slice at the nearest X.
V_slice = squeeze(V(:, idx(i), :));
[Y_grid, Z_grid] = meshgrid(1:10, 1:10);
% Linear interpolation on the slice along Y and Z
Vq(i) = interp2(Y_grid, Z_grid, V_slice, Yq(i), Zq(i), 'linear');
end
% Display the interpolated values
disp('Interpolated values at query points:');
Interpolated values at query points:
disp(Vq);
0.7284 -0.4619 -1.1949

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by