Open In App

3D Array Interpolation MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Interpolation is a method of finding the value of queried data points based on the trend created by existing data points. MATLAB provides many options to perform interpolation on data of N-dimensions. 

In this article, we shall discuss how to interpolate data in a 3D array with the help of some examples. We shall use the interpn() function of MATLAB to perform the interpolation.

Syntax

vq = interpn(x1, x2, x3, V, x1q, x2q, x3q)

x1, x2, x3 = Domain

V = 3D data corresponding to x1, x2, x3

x1q, x2q, x3q = Query points

The above syntax is for 3D data and can be further extended to N dimensions. Let us get the gist of this with the help of examples.

We will create 3 dummy domain data points and then, shall create an ndgrid of the same for corresponding data. Then, we shall query a given data for interpolation.

Example 1:

Matlab




% creating domain points
x1 = 1:33;
x2 = 1:25;
x3 = 1:23;
 
%creating range points using random numbers
Y = rand(33,25,23);
 
%creating query points for interpolation
q1 = [1.3 4 9.5];
q2 = [3 3.6 19];
q3 = [13 13 17.3];
 
%performing interpolation
V = interpn(x1,x2,x3,Y,q1,q2,q3);
disp(V)


Here, we define our domain in \mathbb{R}^3 and then create some random data points in the form of a 3D array and then, create some query points, lying within the defined domain. After performing the interpolation, we get the interpolated points in vector V.

Output:

 

In this example, we shall see how to set the extrapolation value when the queried data points lie outside the defined domain. We shall use the same code example as above and just change the query points. 

Example 2:

Matlab




% Code
% creating domain points
x1 = 1:33;
x2 = 1:25;
x3 = 1:23;
 
%creating range points using random numbers
Y = rand(33,25,23);
 
%creating query points for interpolation
q1 = [1.3 0 -9.5];
q2 = [3 3.6 1];
q3 = [3 -13 17.3];
 
%performing interpolation
V = interpn(x1,x2,x3,Y,q1,q2,q3,'cubic',-1);
disp(V)


Here we have changed the queried data points in a way that the first column lies in the domain while the other lie outside. Then, in the interpn function, after giving the interpolation method, which is necessary because of syntactical requirements, we pass the last argument, the extrapolation value to -1. This means that whenever there will be a query for extrapolation, we shall get -1 instead of NaN.the 

Output:

 



Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads