更新时间:2023-01-27 22:57:34
要完成此任务,除了计算插值,还要完成插值的整个过程.这是从 Octave c ++源代码翻译而来的解决方案.输入格式与 interpn 的第一签名相同函数,除了不需要v
数组.同样,X
应该是向量,并且不应该是ndgrid
格式.输出W
(权重)和I
(位置)都具有(a ,b)
的大小,其中a
是网格上点的邻居数,而b
是要内插的请求点数
To do the task the whole process of interpolation ,except computing the interpolated values, should be done. Here is a solution translated from the Octave c++ source. Format of the input is the same as the frst signature of the interpn function except that there is no need to the v
array. Also X
s should be vectors and should not be of the ndgrid
format. Both the outputs W
(weights) and I
(positions) have the size (a ,b)
that a
is the number of neighbors of a points on the grid and b
is the number of requested points to be interpolated.
function [W , I] = lininterpnw(varargin)
% [W I] = lininterpnw(X1,X2,...,Xn,Xq1,Xq2,...,Xqn)
n = numel(varargin)/2;
x = varargin(1:n);
y = varargin(n+1:end);
sz = cellfun(@numel,x);
scale = [1 cumprod(sz(1:end-1))];
Ni = numel(y{1});
index = zeros(n,Ni);
x_before = zeros(n,Ni);
x_after = zeros(n,Ni);
for ii = 1:n
jj = interp1(x{ii},1:sz(ii),y{ii},'previous');
index(ii,:) = jj-1;
x_before(ii,:) = x{ii}(jj);
x_after(ii,:) = x{ii}(jj+1);
end
coef(2:2:2*n,1:Ni) = (vertcat(y{:}) - x_before) ./ (x_after - x_before);
coef(1:2:end,:) = 1 - coef(2:2:2*n,:);
bit = permute(dec2bin(0:2^n-1)=='1', [2,3,1]);
%I = reshape(1+scale*bsxfun(@plus,index,bit), Ni, []).'; %Octave
I = reshape(1+sum(bsxfun(@times,scale(:),bsxfun(@plus,index,bit))), Ni, []).';
W = squeeze(prod(reshape(coef(bsxfun(@plus,(1:2:2*n).',bit),:).',Ni,n,[]),2)).';
end
测试:
x={[1 3 8 9],[2 12 13 17 25]};
v = rand(4,5);
y={[1.5 1.6 1.3 3.5,8.1,8.3],[8.4,13.5,14.4,23,23.9,24.2]};
[W I]=lininterpnw(x{:},y{:});
sum(W.*v(I))
interpn(x{:},v,y{:})
感谢@SardarUsama的测试和他的有用评论.
Thanks to @SardarUsama for testing and his useful comments.