Skip to content

MATLAB - 3D Array Interpolation

Three-dimensional (3D) interpolation is the process of estimating values at arbitrary points in 3D space, given a set of known data points arranged on a grid. This technique is fundamental in many scientific and engineering fields, such as medical imaging (e.g., upscaling an MRI scan), computational fluid dynamics (CFD), and geophysical data analysis. It allows you to create smoother visualizations, increase data resolution, or query values at locations not explicitly measured.

MATLAB’s primary tool for this task is the interp3 function.

Vq = interp3(X, Y, Z, V, Xq, Yq, Zq)
Vq = interp3(V, Xq, Yq, Zq)
Vq = interp3(V, k)
Vq = interp3(___, method)
Vq = interp3(___, method, extrapolation_val)
  • Vq = interp3(X,Y,Z,V,Xq,Yq,Zq): This is the most explicit form. It interpolates the data in the 3D array V (defined at grid coordinates X,Y,Z) to find the values Vq at the new query coordinates Xq,Yq,Zq.
  • Vq = interp3(V,Xq,Yq,Zq): A simpler form that assumes a default sample grid (1:size(V,2), 1:size(V,1), 1:size(V,3)).
  • Vq = interp3(V,k): Refines the grid by a factor of 2^k. For example, k=1 inserts one new point between each existing sample point in all three dimensions.
  • ... , method): Specifies the interpolation algorithm. Common choices include:
    • 'linear' (Default): Fast and reasonably accurate. A good first choice.
    • 'nearest': Fastest, but produces blocky, discontinuous output.
    • 'cubic': Piecewise cubic interpolation. Produces smoother results than linear but is more computationally intensive.
    • 'spline': Spline interpolation. Generally the smoothest results, but also the most computationally demanding.
    • 'makima': A modified Akima cubic interpolation that avoids the overshoots sometimes seen with 'cubic'.

Let’s explore interp3 with practical examples. Note that we will describe the plots in text, as images are not used.

Here we create a coarse 3D dataset and use interp3 to generate a finer, smoother version.

% 1. Define a coarse grid and sample data
[X, Y, Z] = meshgrid(1:5, 1:5, 1:5);
V = Z.^2 .* sqrt(X.^2 + Y.^2);
% 2. Define a finer query grid
[Xq, Yq, Zq] = meshgrid(1:0.25:5, 1:0.25:5, 1:0.25:5);
% 3. Perform interpolation using the 'cubic' method for a smooth result
Vq = interp3(X, Y, Z, V, Xq, Yq, Zq, 'cubic');
% 4. Visualize the results
figure;
% Plot a slice of the original, coarse data
subplot(1, 2, 1);
slice(X, Y, Z, V, 3, 3, 3);
shading flat; % Use flat shading to emphasize grid cells
title('Original Coarse Data');
xlabel('X'); ylabel('Y'); zlabel('Z');
% Plot a slice of the interpolated, fine data
subplot(1, 2, 2);
slice(Xq, Yq, Zq, Vq, 3, 3, 3);
shading interp; % Use interpolated shading for a smooth look
title('Cubic Interpolated Data');
xlabel('X'); ylabel('Y'); zlabel('Z');

Expected Output Description: This code generates a figure with two subplots. The left plot, ‘Original Coarse Data’, shows a 3D slice view of the data that appears blocky, as it’s rendered on a sparse 5x5x5 grid. The right plot, ‘Cubic Interpolated Data’, shows the same slice but is much smoother and more detailed, demonstrating the effect of interpolating onto a dense 21x21x21 grid.

Example 2: Comparing Interpolation Methods

Section titled “Example 2: Comparing Interpolation Methods”

This example visualizes the difference between ‘linear’ and ‘spline’ interpolation on a simple dataset.

% 1. Create a simple 3D dataset with a single 'hotspot'
V = zeros(5, 5, 5);
V(3, 3, 3) = 1; % A single point of value 1 in a field of zeros
% 2. Interpolate using the 'linear' method
Vq_linear = interp3(V, 2); % Refine the grid twice
% 3. Interpolate using the 'spline' method
Vq_spline = interp3(V, 2, 'spline');
% 4. Visualize the results
figure;
% Linear interpolation result
subplot(1, 2, 1);
slice(Vq_linear, 9, 9, 9); % Slice through the center of the refined grid
title('Linear Interpolation');
xlabel('X'); ylabel('Y'); zlabel('Z');
% Spline interpolation result
subplot(1, 2, 2);
slice(Vq_spline, 9, 9, 9);
title('Spline Interpolation');
xlabel('X'); ylabel('Y'); zlabel('Z');

Expected Output Description: This code produces two subplots. The ‘Linear Interpolation’ plot on the left shows a pyramid-like shape, where the influence of the single hotspot decreases linearly. The ‘Spline Interpolation’ plot on the right shows a much smoother, bell-shaped distribution, illustrating how spline creates a more continuous and flowing transition from the hotspot.

  • Choosing a Method: Start with 'linear' for a good balance of speed and quality. Use 'cubic', 'makima', or 'spline' when visual smoothness is critical. Use 'nearest' only if you need to preserve the original data values and do not want any new values created.
  • Performance: Interpolating large 3D arrays is computationally expensive. If performance is an issue, consider interpolating only a subset of your data or using a lower-resolution query grid.
  • Extrapolation: Be cautious when query points (Xq,Yq,Zq) lie outside the bounds of the original grid (X,Y,Z). interp3 will return NaN (Not a Number) for these points by default. You can provide an optional fifth argument to specify a constant value for extrapolated points, e.g., interp3(..., 'linear', 0) to return 0 for points outside the grid.