在 MATLAB 中编写线性插值函数
Writing a linear interpolation function in MATLAB
我正在尝试在 matlab 中编写线性插值脚本。如何解决向量长度不同的问题?
我试图解决这个问题,但似乎无法理解。是否应该在 for 循环中的任何位置包含 if 语句?
z = linspace(0,1000,21)
vel = 1500*z^0.1;
% I want to interpolate vel between the 201 elements of depths.
depths = [0:5:1000];
numel_depths = numel(depths);
for ii = 1:numel_depths %Going through all indices of depths
lower = find(z > depths(ii),1); % Finding x0 and x1.
higher = find(z < depths(ii),1,'last');
V2(ii) = vel(lower) + ((vel(higher) - vel(lower))/(z(higher)-
z(lower)))*(depths(ii)-z(lower)); % linear interpolation step
end
现在 returns 一个错误,指出不同的边有不同数量的元素。有没有办法解决这个问题,使其像 MATLAB 中已安装的 interp1 函数一样工作?
您的代码存在几个问题:
你需要元素级幂运算符来定义vel
,正如你一定看到的错误信息所指示的:
vel = 1500*z.^0.1; % Use .^ rather than ^
如果您正在测试的范围内没有任何点,则 lower
或 upper
有可能为空。您需要使用如下测试跳过这些案例:
~isempty( lower ) && ~isempty( higher )
您没有使用有效的续行符。在 MATLAB 中你不能只打断一行,你必须在虚线的末尾添加省略号 (...
)。
您正在使用严格的不等式 >
和 <
,这意味着您错过了应该包含在插值中的边界点(下面的演示)。
你应该在 for
循环之前预先分配数组,所以
v2 = NaN(size(depths)); % Define output V2 to be same size as input depths
lower
是MATLAB内置函数,用于将字符串转为小写,避免将其用作变量名。
以上所有问题的修复如下:
z = linspace(0,1000,21);
vel = 1500*z.^0.1; % Element-wise power operator
depths = 0:5:1000;
V2 = NaN(size(depths)); % Pre-allocate output array
for ii = 1:numel(depths);
% Finding x0 and x1, inclusive of boundaries (>= or <=).
% Using 'x0' and 'x1' as var names to avoid shadowing the 'lower' function
x0 = find(z >= depths(ii),1);
x1 = find(z <= depths(ii),1,'last');
% Check if any points fell in this region
if ~isempty( x0 ) && ~isempty( x1 )
% Interpolation step, note the line continuation "..."
V2(ii) = vel(x0) + ((vel(x1) - vel(x0))/(z(x1) - ...
z(x0)))*(depths(ii)-z(x0));
end
end
我们可以根据内置插值函数验证这一点 interp1
- 你原来的严格不等式
<
和>
:
- 具有如上所示的非严格不等式
<=
和 >=
:
我正在尝试在 matlab 中编写线性插值脚本。如何解决向量长度不同的问题?
我试图解决这个问题,但似乎无法理解。是否应该在 for 循环中的任何位置包含 if 语句?
z = linspace(0,1000,21)
vel = 1500*z^0.1;
% I want to interpolate vel between the 201 elements of depths.
depths = [0:5:1000];
numel_depths = numel(depths);
for ii = 1:numel_depths %Going through all indices of depths
lower = find(z > depths(ii),1); % Finding x0 and x1.
higher = find(z < depths(ii),1,'last');
V2(ii) = vel(lower) + ((vel(higher) - vel(lower))/(z(higher)-
z(lower)))*(depths(ii)-z(lower)); % linear interpolation step
end
现在 returns 一个错误,指出不同的边有不同数量的元素。有没有办法解决这个问题,使其像 MATLAB 中已安装的 interp1 函数一样工作?
您的代码存在几个问题:
你需要元素级幂运算符来定义
vel
,正如你一定看到的错误信息所指示的:vel = 1500*z.^0.1; % Use .^ rather than ^
如果您正在测试的范围内没有任何点,则
lower
或upper
有可能为空。您需要使用如下测试跳过这些案例:~isempty( lower ) && ~isempty( higher )
您没有使用有效的续行符。在 MATLAB 中你不能只打断一行,你必须在虚线的末尾添加省略号 (
...
)。您正在使用严格的不等式
>
和<
,这意味着您错过了应该包含在插值中的边界点(下面的演示)。你应该在
for
循环之前预先分配数组,所以v2 = NaN(size(depths)); % Define output V2 to be same size as input depths
lower
是MATLAB内置函数,用于将字符串转为小写,避免将其用作变量名。
以上所有问题的修复如下:
z = linspace(0,1000,21);
vel = 1500*z.^0.1; % Element-wise power operator
depths = 0:5:1000;
V2 = NaN(size(depths)); % Pre-allocate output array
for ii = 1:numel(depths);
% Finding x0 and x1, inclusive of boundaries (>= or <=).
% Using 'x0' and 'x1' as var names to avoid shadowing the 'lower' function
x0 = find(z >= depths(ii),1);
x1 = find(z <= depths(ii),1,'last');
% Check if any points fell in this region
if ~isempty( x0 ) && ~isempty( x1 )
% Interpolation step, note the line continuation "..."
V2(ii) = vel(x0) + ((vel(x1) - vel(x0))/(z(x1) - ...
z(x0)))*(depths(ii)-z(x0));
end
end
我们可以根据内置插值函数验证这一点 interp1
- 你原来的严格不等式
<
和>
:
- 具有如上所示的非严格不等式
<=
和>=
: