Create a function that interpolates matrix values

2 次查看(过去 30 天)
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).

回答(1 个)

Thiago Henrique Gomes Lobato
编辑:Thiago Henrique Gomes Lobato 2019-12-29
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

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by