MATLAB Answers

Gorkem
0

Multiple calls to interpn using same vectors

Asked by Gorkem
on 16 Mar 2018
Latest activity Commented on by Gorkem
on 23 Mar 2018
I do have a bunch of functions defined in the same domain but mapping to different values and I evaluate those functions with interpn as there's no closed form formula.
so lets say, the domain is D1 x D2 x D3 x D4 (all vectors) and each function (F1, F2, F3) maps its 4d arguments to a scalar value.
I'm evaluating each function at the same query points using interpn with linear interpolation like :
f1val=interpn(D1,D2,D3,D4,F1,dq1,dq2,dq3,dq4);
f2val=interpn(D1,D2,D3,D4,F2,dq1,dq2,dq3,dq4);
f3val=interpn(D1,D2,D3,D4,F3,dq1,dq2,dq3,dq4);
I'm guessing that each call would do 4 binary searches to find the nearest index points used to evaluated linear interpolation.
This code is going to be converted to C/C++ using Code generation. I was wondering if the Code generation would avoid doing search for the same query points in all instantiations of interpn. If not, how can I manually ensure that this is not the case?

  0 Comments

Sign in to comment.

1 Answer

Answer by Mike Hosea
on 22 Mar 2018
 Accepted Answer

Unfortunately, there will be a separate look-up for each call. The only thing I can think of is to find the bounding box yourself and then use INTERPN to do the linear interpolation without needing to find it. Something like this.
function [r1,r2,r3] = interpn3times(x1,x2,x3,x4,F1,F2,F3,xi1,xi2,xi3,xi4)
b1 = getBracket(x1,xi1);
b2 = getBracket(x2,xi2);
b3 = getBracket(x3,xi3);
b4 = getBracket(x4,xi4);
r1 = interpn(x1(b1),x2(b2),x3(b3),x4(b4),F1(b1,b2,b3,b4),xi1,xi2,xi3,xi4);
r2 = interpn(x1(b1),x2(b2),x3(b3),x4(b4),F2(b1,b2,b3,b4),xi1,xi2,xi3,xi4);
r3 = interpn(x1(b1),x2(b2),x3(b3),x4(b4),F3(b1,b2,b3,b4),xi1,xi2,xi3,xi4);
function b = getBracket(v,x)
L = length(v);
if x >= v(end)
b = [L-1,L];
elseif x <= v(1)
b = [1,2];
else
i = interp1(v,1:L,x,'previous');
b = [i,i+1];
end
I've tested this with exactly one test case, so obviously this is a no guarantees, use at your own risk type of thing, and I'm not sure it will actually be faster to do it this way than just to call INTERPN and let it do the look-ups three times. But that's the only thing that came to mind other than hacking the generated C code.

  1 Comment

Thanks much Mike. I'll test it in my code.

Sign in to comment.