Create a function that interpolates matrix values

조회 수: 12 (최근 30일)
Frank Rosler
Frank Rosler 2019년 12월 29일
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일
편집: 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