Interpolate a 3D-Matrix using interp1

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 3x3 Matrix with the values 1.5 but i just get an error.
Thanks a lot!

 Respuesta aceptada

Matt J
Matt J el 8 de Dic. de 2012
Editada: Matt J el 8 de Dic. de 2012
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.

Más respuestas (1)

Glynn
Glynn el 15 de En. de 2014

0 votos

Using the same example above, what if the xi in interp1(x,Y,xi), is a 2-d matrix the same size as the first two dimensions of 'matrix'? I keep getting OOM issues when trying to solve: x is a (1,3)array Y is a (2030,873,3) matrix xi is a (2030,873) matrix

3 comentarios

Matt J
Matt J el 15 de En. de 2014
Editada: Matt J el 16 de En. de 2014
interp1(x,Y,xi) will interpolate each column of Y at all points specified in xi. In your case, that means it is trying to generate a resulting array containing (2030*873)^2 values. Thus your out-of-memory errors.
You cannot use interp1(x,Y,xi) to do 1D interpolation at a different set of points in every column of Y. You would have to do that as a multi-dimensional interpolation.
Glynn
Glynn el 16 de En. de 2014
Okay got it, thanks, makes sense. So should I be using interp3 instead? I'm confused as how to set that up with my particular example. Any help is appreciated.
Matt J
Matt J el 16 de En. de 2014
Editada: Matt J el 16 de En. de 2014
Yes, you could use interp3, though I think griddedInterpolant is easier to work with. I don't actually have a clear picture how your xi are supposed to be interpreted. If you are interpolating in a 3D array, like Y, then the locations at which you interpolate must consist of (xx,yy,zz) coordinate triplets.

Iniciar sesión para comentar.

Categorías

Más información sobre Interpolation en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 8 de Dic. de 2012

Editada:

el 16 de En. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by