MATLAB: How to interpolate a data set given by 5 “inputs”

interpninterpolation

I have a question related to an interpolation process. My data set is given in the following way:
An array Y which contains several vectors as follows:
Y = [Y1; Y2; Y3; Y4; Y5; Y6; …. Y32];
Each of these vector is defined as: Y1 = [0,0,0,0,0], Y2 = [0,0,0,0,1],….
Then, every Y_i vector has an evaluated parameter. For instance, V(Y1) = 80, V(Y2) = 90 leading to a vector V whose length is 32.
My goal is to get the value for any configuration of the Y vector, for example, xq = [0,0,0.5,0,1].
I tried it via the interpn function as follows:
vq = interpn(Y,V,xq,'linear');
Where,
[x1,x2,x3,x4,x5] = ndgrid(0:0.5:1);
xq = [x1(:) x2(:) x3(:) x4(:) x5(:)];
But I obtained an error using griddedInterpolant/subsref -> "The input data has inconsistent size".
Is it possible to carry out this kind of interpolation? Thanks in advance.

Best Answer

TLDR: The Y and xq you've constructed work for scatteredInterpolant but not for griddedInterpolant which uses a different format.
interpn expects gridded data in a full grid format, which is not what your Y represents, at least in its current form. To represent gridded data, you would have to pass either 5 vectors (each [0 1] it sounds) or 5 5-D matrices
Assuming your Y truly represents all distincts point of a full grid (which it should be if you have 32 values) you can transform your input data into the format required by interpn (or griddedInterpolant which I would recommend over interpn):
griddedvalues = accumarray(Y + 1, V); %only works if Y values are integers. + 1 to make the 0 valid indices
You can then create your interpolant with
interpolant = griddedInterpolant(repmat({[0 1]}, 1, 5), griddedvalues);
and query it as
result = interpolant(x1, x2, x3, x4, x5)
Unfortunately, with gridded interpolant (and interpn you can't pass the query point in the xq format you created.
---
The syntax you've used however works with scatteredInterpolant.
interpolant = scatteredInterpolant(Y, V);
result = interpolant(xq)
scatteredInterpolant is probably slower than griddedInterpolant for properly gridded data but it's certainly easier to use with your Y and xq.