MATLAB: Interpolate a 3D-Matrix using interp1

3d matrixinterp1interpolationMATLAB

I try to interpolate a 3D Matrix using interp1. I think it should be easy but i just don't get it.
I have a 3d-matrix and a vector in z direction. I wan't to get an interpolated 2d Matrix for any value in the z direction. For Example:
matrix(1:3,1:3,1) = 1
matrix(1:3,1:3,2) = 2
z(1,1,1:2) = [10,20]
interp1(z,matrix,15)
so I thought interp1 would return a 3×3 Matrix with the values 1.5 but i just get an error.
Thanks a lot!

Best Answer

V=reshape(matrix,[],2).';
VI = interp1(z,V,15);
ans = reshape(VI,3,3),
You could also use griddedInterpolant instead
F=griddedInterpolant({1:3,1:3,z},matrix);
ans= F({1:3,1:3,15});
It's hard to say which would be more efficient, in general. griddedInterpolant is a newer and better optimized function, but interp1 takes advantage of the 1D nature of the interpolation.
Related Question