Multiple calls to interpn using same vectors

3 views (last 30 days)
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 :
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?

Accepted Answer

Mike Hosea
Mike Hosea on 22 Mar 2018
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];
i = interp1(v,1:L,x,'previous');
b = [i,i+1];
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.

More Answers (0)


Find more on Interpolation in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!