Create a function that interpolates matrix values
10 ビュー (過去 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).
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!