Skip to content

MATLAB - 2D Array Interpolation

In MATLAB, 2D interpolation is the process of estimating values at new points within a grid of known sample points. It’s a critical technique for tasks like resizing images, upsampling data for smoother visualizations, and filling in missing data. The primary function for this is interp2.

Vq = interp2(X, Y, V, Xq, Yq, method)
  • X, Y, V: These define the original data. V is a matrix of values, and X and Y are corresponding coordinate matrices (often created with meshgrid).
  • Xq, Yq: These are the coordinates of the query points where you want to estimate values.
  • Vq: The output matrix of interpolated values at the query points.
  • method: A string specifying the interpolation algorithm. This is a crucial choice that affects the quality and speed of the result.

Your choice of method depends on your data and your goals:

MethodDescriptionBest For
'linear' (Default)Fast, piecewise linear interpolation. Connects points with straight lines.General purpose, good balance of speed and smoothness.
'cubic'Slower, piecewise cubic interpolation. Produces a smoother surface.Visualizations, when smoothness is critical. Can sometimes overshoot values.
'spline'Smooth and continuous, based on cubic splines. High computational cost.High-quality, smooth results without the overshooting of 'cubic'.
'nearest'Very fast. Assigns the value of the nearest known point. Creates a blocky look.Preserving original data values, categorical data, or when speed is paramount.
'makima'A modified Akima cubic Hermite method. Smooth like cubic, but avoids overshoots.Data with sharp changes or flat regions where overshooting is undesirable.

Let’s create a coarse surface and interpolate it onto a finer grid to create a smoother plot.

% 1. Create a coarse grid and sample data
[X, Y] = meshgrid(1:5);
V = peaks(X, Y);
% 2. Create a finer query grid
[Xq, Yq] = meshgrid(1:0.25:5);
% 3. Perform interpolation using different methods
Vq_linear = interp2(X, Y, V, Xq, Yq, 'linear');
Vq_cubic = interp2(X, Y, V, Xq, Yq, 'cubic');
Vq_nearest = interp2(X, Y, V, Xq, Yq, 'nearest');
% 4. Visualize the results
figure;
tiledlayout(2, 2);
% Original Surface
nexttile;
surf(X, Y, V);
title('Original Coarse Surface');
% Linear Interpolation
nexttile;
surf(Xq, Yq, Vq_linear);
title('Linear Interpolation');
% Cubic Interpolation
nexttile;
surf(Xq, Yq, Vq_cubic);
title('Cubic Interpolation');
% Nearest-Neighbor Interpolation
nexttile;
surf(Xq, Yq, Vq_nearest);
title('Nearest-Neighbor Interpolation');

Example 2: Resampling without Explicit Grid Coordinates

Section titled “Example 2: Resampling without Explicit Grid Coordinates”

If your data V corresponds to a default grid (X = 1:size(V,2), Y = 1:size(V,1)), you can omit X and Y from the call. interp2(V, k) provides a convenient shortcut for resampling by an integer factor k.

% 1. Create a sample data matrix
V = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
% 2. Refine the data by a factor of 4 (k=4)
% This interpolates 3 new points between each sample
k = 4;
Vq = interp2(V, k, 'cubic'); % Using cubic for a smooth result
% 3. Plot the results
figure;
tiledlayout(1, 2);
nexttile;
imagesc(V);
axis equal tight;
title('Original 4x4 Grid');
nexttile;
imagesc(Vq);
axis equal tight;
title('Interpolated 17x17 Grid (k=4)');

When you need to perform many interpolations on the same dataset, interp2 can be inefficient because it recomputes the triangulation each time. For these scenarios, the modern and highly recommended approach is to use griddedInterpolant.

% 1. Create the interpolant object once
[X, Y] = meshgrid(-3:0.2:3);
Z = peaks(X, Y);
F = griddedInterpolant(X, Y, Z, 'cubic');
% 2. Query the object as many times as needed
[Xq, Yq] = meshgrid(-3:0.05:3);
Zq = F(Xq, Yq);
% This is much faster than calling interp2 repeatedly in a loop.
  • Query Points Outside the Grid: By default, interp2 returns NaN for any query point (Xq, Yq) that lies outside the domain of the original sample points (X, Y). You can specify an extrapolation value if needed, e.g., interp2(..., 'linear', 0) to return 0 for points outside the grid.
  • Monotonic Grid Vectors: The coordinate vectors used to define your grid (X and Y in meshgrid) must be monotonic (strictly increasing or decreasing).
  • Choosing the Wrong Method: Using 'cubic' or 'spline' on data with sharp discontinuities can lead to undesirable ringing or oscillations. 'linear' or 'makima' are often better choices in such cases.

Try loading a standard MATLAB demo image (e.g., I = imread('peppers.png');). Convert it to grayscale and downsize it. Then, use interp2 with 'nearest', 'linear', and 'cubic' methods to scale it back to its original size. Compare the results to see the practical impact of each interpolation algorithm.