weight.m

来自「计算水声传播的快速场(FFP)程序」· M 代码 · 共 38 行

M
38
字号
function [ w, Ix ] = weight( x, xTab )

% Computes weights for linear interpolation
%     Given 
%        x(*)    abscissas
%        xTab(*) points for tabulation
%
%     Compute
%        w(*)    weights for linear interpolation
%        Ix(*)   indices for    "         "
%
% routine can fail if x is not strictly monotonically increasing

% pre-allocate output vectors for efficiency
Nx    = length( x    );
NxTab = length( xTab );
Ix    = zeros( NxTab, 1 );
w     = zeros( NxTab, 1 );

% Quick return if just one X value for interpolation ***

if ( Nx == 1 )
    w(  1 ) = 0.0;
    Ix( 1 ) = 1;
    return
end

L = 1;

for IxTab = 1 : NxTab   % Loop over each point for which the weights are needed
    
    % search for index, L, such that [X(L), X(L+1)] brackets rcvr depth
    while ( xTab( IxTab ) > x( L + 1 ) && L < Nx - 1 )
        L = L + 1;
    end
    Ix( IxTab ) = L;                                                    % here's the output index
    w(  IxTab ) = ( xTab( IxTab ) - x( L ) ) / ( x( L+1 ) - x( L ) );   % here's the output weight
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?