How to interpolate on a 2D array in MATLAB

How can I make a function of 2 variables and get a 2D array, will it return the interpolated value?

I have an N x M array A I need to interpolate it and somehow get a function of this surface so that I can select values โ€‹โ€‹for non-integer arguments. (I need to use this interpolation as a function of two variables)

For instance:

 A[N,M] //my array // here is the method I'm looking for. Returns function interpolatedA interpolatedA(3.14,344.1) //That function returns interpolated value 
+6
source share
4 answers

For regular grid data, use interp2 . If your data is scattered, use griddata . You can create an anonymous function as a simplified wrapper around these calls.

 M = 10; N = 5; A = rand(M,N); interpolatedA = @(y,x) interp2(1:N,1:M,A,x,y); %interpolatedA = @(y,x) griddata(1:N,1:M,A,x,y); % alternative interpolatedA(3.3,8.2) ans = 0.53955 
+4
source

Here is an example using scatteredInterpolant :

 %# get some 2D matrix, and plot as surface A = peaks(15); subplot(121), surf(A) %# create interpolant [X,Y] = meshgrid(1:size(A,2), 1:size(A,1)); F = scatteredInterpolant(X(:), Y(:), A(:), 'linear'); %# interpolate over a finer grid [U,V] = meshgrid(linspace(1,size(A,2),50), linspace(1,size(A,1),50)); subplot(122), surf(U,V, F(U,V)) 

pic

Please note that you can evaluate the interpolation object at any point:

 >> F(3.14,3.41) ans = 0.036288 

the above example uses a vector call to interpolate at all grid points

+6
source

Have you seen the interp2 function?

From the MatLab documentation:

ZI = interp2(X,Y,Z,XI,YI) returns a ZI matrix containing the elements corresponding to the elements XI and YI , and is determined by interpolation in the two-dimensional function given by the matrices X , Y and Z , X and Y must be monotonic and have same format ("plaid"), as if they were created using meshgrid . Matrices X and Y indicate the points at which data Z is given. Out-of-range values โ€‹โ€‹are returned as NaN s.

0
source

Use the spline() command as follows:

 % A contains N rows and 2 columns pp = spline(A(:,1), A(:,2)); ppval(pp,3.44) ans = 0.4454 
0
source

Source: https://habr.com/ru/post/944158/


All Articles