我以粗略的方式实现了这一点(我不经常使用 Octave 或 Matlab,所以我不习惯向量和矩阵数据类型以及如何充分利用它们)。
该函数接受 X 轴值,需要一个加权矩阵,其中第一列包含 x 值,第二列包含加权因子。如果提供的 X 轴值超出加权矩阵的范围,它还需要选择两个标准值。
它返回加权因子的线性插值,或者如果加权矩阵格式错误(一个 x 点的多个条目),则返回较大的加权因子。
这太可怕了,没有检查,除了我以外的每个人都可能失败。也许这是其他人的起点。
function retval = Weighting ( x, weightingMatrix, weightingBelowMinimum, weightingAboveMaximum)
if (x < weightingMatrix(1,1))
retval = weightingBelowMinimum;
elseif ( x > weightingMatrix(size(weightingMatrix,1),1))
retval = weightingAboveMaximum;
else
smallerWeightIndex = 0;
biggerWeightIndex = 0;
for i=1:size(weightingMatrix,1)
if (x<weightingMatrix(i,1))
biggerWeightIndex = i;
smallerWeightIndex = i-1;
break;
endif
endfor
if (weightingMatrix(biggerWeightIndex,2)==weightingMatrix(smallerWeightIndex,2))
retval = weightingMatrix(biggerWeightIndex,2);
elseif (weightingMatrix(biggerWeightIndex,1)==weightingMatrix(smallerWeightIndex,1))
retval = max(weightingMatrix(biggerWeightIndex,2),weightingMatrix(smallerWeightIndex,2));
else
m = (weightingMatrix(biggerWeightIndex,2)-weightingMatrix(smallerWeightIndex,2))/(weightingMatrix(biggerWeightIndex,1)-weightingMatrix(smallerWeightIndex,1));
c = weightingMatrix(biggerWeightIndex,2)-(m*weightingMatrix(biggerWeightIndex,1));
retval = m*x+c;
endif
endif
endfunction