Create a function that interpolates matrix values
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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).
0 commentaires
Réponses (1)
Thiago Henrique Gomes Lobato
le 29 Déc 2019
Modifié(e) : Thiago Henrique Gomes Lobato
le 29 Déc 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
0 commentaires
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!