Help with coding a bilinear interpolation function

My specific problem involves taking, let's say, matrix1, which contains a n*m two dimensional array with a variable set to a certain value at each point, and interpolating the variable values at each point to points in matrix2, which contains a finer mesh of points in the same domain n*m.
I found that for my needs, I will be using a bilinear interpolation, but have some questions in how to code in within MatLab. First, is there any function to easily do this, or if not, how could I find and identify the four nearest points (in the image, 1, 2, 3 and 4) in matrix1 that bound a single point in matrix2 (5)?
Image for reference: https://imgur.com/jfiMtYf
After that, I think I could manage coding the bilinear interpolation itself (finding the two x-axis points and using those to find the point in matrix2).
Thanks for any answers in advance for my question about identifying the four bounding points.

 Respuesta aceptada

Matt J
Matt J el 19 de Jul. de 2018
You can just use interp2() or griddedInterpolant().

7 comentarios

Taras Goral
Taras Goral el 19 de Jul. de 2018
Editada: Taras Goral el 19 de Jul. de 2018
I tried using interp2() but ran into some question with the syntax. I have two tables, t1random
and t2random,
that have x-values in the 1st column, y in the 2nd, and t in the 3rd.
I tried the following syntax
Vq=interp2(t1random(1:end,1), t1random(1:end,2), t1random(1:end,3), t2random(1:end,1), t2random(1:end,2))
but ran into an error. Any help with the syntax? The excel spreadsheets were improted without problem.
t1_2D = reshape(t1random,10,[]);
Vq=interpn(t1_2D, t2random(:,1)+1, t2random(:,2)+1 )
Taras Goral
Taras Goral el 19 de Jul. de 2018
Thank you for your help with this, your solution worked and I also ended up finding out how to set up meshgrids for the interp2() function. Just out of curiosity (it wouldn't be practical, of course), would you know how to manually solve the problem of finding the four nearest bounding points for a point in the second matrix?
Matt J
Matt J el 19 de Jul. de 2018
Editada: Matt J el 19 de Jul. de 2018
You could use the discretize() command. It's easiest to demonstrate in 1D, but extends easily to higher dimensions:
>> grid=[-.2,-.1,0,.1,.2];
>> where = discretize( .04 , grid)
where =
3
So as you can see, the command determined that .04 lay between bounding points grid(3) and grid(4).
Incidentally, meshgrids aren't necessary for interp2 or griddedInterpolant. Because they take up extra memory, you would only use them if you had them lying around from some previous computation.
Taras Goral
Taras Goral el 27 de Jul. de 2018
Editada: Taras Goral el 27 de Jul. de 2018
I had some trouble using the reshape function with the table created, could you explain what form the table needs to be in for the interp2() function to work? I attached a screenshot to show how the table is laid out.
Matt J
Matt J el 27 de Jul. de 2018
Editada: Matt J el 27 de Jul. de 2018
Your link is broken for some reason. Why not just embed your image in your comments? That would be the easiest.
Sorry for that, here's the image:

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Versión

R2018a

Preguntada:

el 19 de Jul. de 2018

Comentada:

el 1 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by