MATLAB: Giving interpn vector inputs of unknown dimension

interpolationmatlab function

I have been struggling to apply interpn to sets of data of unknown dimension without using a bunch of if statements – not very pretty. I will use the notation from the Matlab documentation rather than copy pasting my codes. Quoting the documentation for interpn:
  • Vq = interpn(X1,X2,…,Xn,V,Xq1,Xq2,…,Xqn)
  • X1,X2,…,Xn contain the coordinates of the sample points.
  • V contains the corresponding function values at each sample point.
  • Xq1,Xq2,…,Xqn contain the coordinates of the query points.
For my problem, the dimension n of the interpolation is not fixed. I have a cell X, containing the coordinates of the sample points X1,…,Xn, an n-dimensional vector V, and the vector of query points Xq. Is there a way to split a vector/cell to be given as individual arguments for interpn (or any function for that matter)? So that I could write something like
Vq = interpn(split(X),V,split(Xq))
and Matlab would read in split(X) as X1,X2,…,Xn? My current implementation is very clunky, and is limited to a finite set of n:
if (numel(X) == 1)
out = interpn(X{1},V,Xq(:,1),'linear');
elseif (numel(X) == 2)
out = interpn(X{1},X{2},V,Xq(:,1),Xq(:,2),'linear');
elseif (numel(X) == 3)
out = interpn(X{1},X{2},X{3},V,Xq(:,1),Xq(:,2),Xq(:,3),'linear');
end
Any advice would be great. Thanks.

Best Answer

XqC = num2cell(Xq,1); %splits into columns
out = interpn(X{:}, V, XqC{:}, 'linear')