Matlab:在整个矩阵上插入 4 个角值
Matlab: interpolate 4 corner values over entire matrix
我在 Matlab 中有一个 20x23 的矩阵,它的 4 个角有一个值:
(1,1) = 16.46
(1,23) = 16.16
(20,1) = 16.93
(20,23) = 16.57
如何在整个 20x23 域内插入这些值?
谢谢!
在此示例中,我使用了 'linear'
插值。下面我引用矩阵作为样本点,我将矩阵的角定义为坐标 (1,1)、(1,2)、(2,1) 和 (2,2)。要插入 Matrix
的值,必须创建两个向量来定义查询点。这就像一个更精细的网格(空间域),告诉 interp2()
函数要插值的点(中间)。这当然是一种内置方法,还有其他方法。
可用于interp2()
的一些插值方法: 'linear'
,'nearest'
,'cubic'
,'spline'
, 'makima'
Matrix(1,1) = 16.46; %Top-left corner%
Matrix(1,2) = 16.16; %Top-right corner%
Matrix(2,1) = 16.93; %Bottom-left corner%
Matrix(2,2) = 16.57; %Bottom-right corner%
Width = 23;
Height = 20;
Finer_X_Points = linspace(1,2,Width);
Finer_Y_Points = linspace(1,2,Height)';
Finer_Matrix = interp2(Matrix,Finer_X_Points,Finer_Y_Points,'linear');
对于附加颜色图:
pcolor(flip(Finer_Matrix,1));
title("Interpolated Matrix");
colorbar
axis off
运行 使用 MATLAB R2019b
你基本上是在做 bilinear interpolation.
的单个单元格
所以从link开始,你需要的公式是
您可以使用 interp2
,但与 Michael 的回答不同,我们也可以自己实现...
% Setup
x = [1, 23];
y = [1, 20];
z = [16.46, 16.93;
16.16, 16.57];
% Interpolation function
biLinearInterp = @(xi,yi) ( 1/( (x(2)-x(1))*(y(2)-y(1)) ) ) * ...
[x(2)-xi, xi-x(1)] * z * [y(2)-yi; yi-y(1)];
% Create data
[X, Y] = meshgrid( x(1):x(2), y(1):y(2) );
Z = arrayfun( @(xi,yi) biLinearInterp(xi,yi), X, Y );
这满足您的原始规格
Z(1,1) = 16.46
Z(1,23) = 16.16
Z(20,1) = 16.93
Z(20,23) = 16.57
中间有线性插值
我在 Matlab 中有一个 20x23 的矩阵,它的 4 个角有一个值:
(1,1) = 16.46
(1,23) = 16.16
(20,1) = 16.93
(20,23) = 16.57
如何在整个 20x23 域内插入这些值?
谢谢!
在此示例中,我使用了 'linear'
插值。下面我引用矩阵作为样本点,我将矩阵的角定义为坐标 (1,1)、(1,2)、(2,1) 和 (2,2)。要插入 Matrix
的值,必须创建两个向量来定义查询点。这就像一个更精细的网格(空间域),告诉 interp2()
函数要插值的点(中间)。这当然是一种内置方法,还有其他方法。
可用于interp2()
的一些插值方法: 'linear'
,'nearest'
,'cubic'
,'spline'
, 'makima'
Matrix(1,1) = 16.46; %Top-left corner%
Matrix(1,2) = 16.16; %Top-right corner%
Matrix(2,1) = 16.93; %Bottom-left corner%
Matrix(2,2) = 16.57; %Bottom-right corner%
Width = 23;
Height = 20;
Finer_X_Points = linspace(1,2,Width);
Finer_Y_Points = linspace(1,2,Height)';
Finer_Matrix = interp2(Matrix,Finer_X_Points,Finer_Y_Points,'linear');
对于附加颜色图:
pcolor(flip(Finer_Matrix,1));
title("Interpolated Matrix");
colorbar
axis off
运行 使用 MATLAB R2019b
你基本上是在做 bilinear interpolation.
的单个单元格所以从link开始,你需要的公式是
您可以使用 interp2
,但与 Michael 的回答不同,我们也可以自己实现...
% Setup
x = [1, 23];
y = [1, 20];
z = [16.46, 16.93;
16.16, 16.57];
% Interpolation function
biLinearInterp = @(xi,yi) ( 1/( (x(2)-x(1))*(y(2)-y(1)) ) ) * ...
[x(2)-xi, xi-x(1)] * z * [y(2)-yi; yi-y(1)];
% Create data
[X, Y] = meshgrid( x(1):x(2), y(1):y(2) );
Z = arrayfun( @(xi,yi) biLinearInterp(xi,yi), X, Y );
这满足您的原始规格
Z(1,1) = 16.46
Z(1,23) = 16.16
Z(20,1) = 16.93
Z(20,23) = 16.57
中间有线性插值