Create a function that interpolates matrix values

5 views (last 30 days)
My problem is the following:
Suppose we have a matrix A. I want to interpret the matrix entries A(i,j) as the values of a function f(x,y) on a lattice in R^2, that is A(i,j)=f(i,j) for all i,j on the lattice.
Is it possible to define such a function f as a Matlab function?
That is, given a matrix A, I want to define a function of the continuous variable (x,y) which somehow interpolates between the values of A (for my purposes, it could even be a piecewise constant function).

Answers (1)

Thiago Henrique Gomes Lobato
Edited: Thiago Henrique Gomes Lobato on 29 Dec 2019
Yes, sure. Something like this should do the trick:
% Your tabular function
A = [1,2;
3,4];
% Your values
x = 1.5;
y = 1.1;
res = ftable(x,y,A)
function funVal = ftable(x,y,A)
% Find the index in the matrix
Xx(1) = floor(x);
Xx(2) = ceil(x);
if Xx(1)== Xx(2)
Xx(2) = Xx(2)+1;
end
Yy(1) = floor(y);
Yy(2) = ceil(y);
if Yy(1)== Yy(2)
Yy(2) = Yy(2)+1;
end
% Get the matrix values
V = A(Xx,Yy);
% Mesh grid and interpolate
[X,Y] = meshgrid(Xx,Yy);
funVal = interp2(X,Y,V,x,y);
end
res =
1.7000

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!