
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Nearest Neighbor Interpolation Algorithm in MATLAB
Nearest-neighbor interpolation algorithm is a simple interpolation method used to interpolate unknown values between data points. We can implement this algorithm in MATLAB programming. Read this article to learn how to implement the nearest-neighbor interpolation algorithm in MATLAB. Let us start our discussion with the basics of nearest-neighbor interpolation algorithm.
What is Nearest-Neighbor Interpolation Algorithm?
The nearest-neighbor interpolation algorithm is a simple method to estimate the unknown values between discrete data points. This algorithm is quite useful in various fields of engineering like image processing, signal processing, etc.
The principle behind this algorithm is very simple, that is, it assigns a value to the closest data point to the interpolated location.
Here are the steps involved in the nearest-neighbor interpolation algorithm
Step 1 Input a set of discrete data points.
Step 2 Specify the points where you want to interpolate the values.
Step 3 For each interpolation point, determine a nearest value.
Step 4 Assign the interpolated value to the nearest-neighbor to the interpolation point.
This is how the nearest-neighbor interpolation algorithm works.
Before discussing the implementation of the nearest-neighbor interpolation algorithm in MATLAB. Let us know its benefits and limitations.
Benefits of Nearest-Neighbor Interpolation Algorithm
Some major advantages of nearest-neighbor interpolation algorithm are listed below
The nearest-neighbor interpolation algorithm is a simple technique to understand and implement.
This algorithm does not affect the original values in the data set.
In image processing, the nearest-neighbor interpolation algorithm does not cause any blurring or smoothing effect.
It can be efficiently applied to interpolate discrete data points like a digital signal.
However, nearest neighbor interpolation algorithm also has some limitations that we need to consider while using it.
Limitations of Nearest-Neighbor Interpolation Algorithm
Some key limitations of nearest-neighbor interpolation algorithm are listed below
Nearest neighbor interpolation algorithm can cause blocky appearance when it is used to increase the resolution of an image.
This algorithm cannot be used to interpolate continuous data sets or functions.
It has limited accuracy in results when using in sophisticated conditions.
This is all about basics of nearest neighbor interpolation algorithm. Where, it is a simple and efficient interpolation technique suitable for discrete data sets.
Implement Nearest-Neighbor Interpolation Algorithm in MATLAB
In MATLAB, we can implement the nearest-neighbor algorithm using the "nearest" option.
Here is the step wise explanation for implementing the nearest-neighbor interpolation in MATLAB
Step 1 Define the input data. It can be a matrix, a digital image, etc.
Step 2 Define the new size or new grid required to interpolate the data.
Step 3 Perform nearest-neighbor interpolation of the input data.
Step 4 Display the results.
These are the generalized steps. However, there can some more steps involved depending on the type of input data.
Example (1) - Resize an image using nearest-neighbor interpolation
Let us understand the nearest-neighbor interpolation algorithm in MATLAB with the help of examples.
% Read the input image img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg'); % Specify new dimensions to resize the image new_size = [400, 250]; % Apply nearest-neighbor interpolation to resize the image new_img = imresize(img, new_size, 'nearest'); % Display the original and resized images figure; subplot(1, 2, 1); imshow(img); title('Original Image'); axis([0, 600, 0, 600]); axis on; subplot(1, 2, 2); imshow(new_img); title('Resized Image'); axis([0, 600, 0, 600]); axis on;
Output
It will produce the following output:

Code Explanation
In this example, we start by reading the input image using the "imread" function. Then, specify the new dimensions to resize the image. After that we use the "imresize" function with the "nearest" option to resize the image using the nearest-neighbor interpolation method.
Finally, we display the input and resized images using the "imshow" function.
Example (2) - Perform interpolation of a matrix using nearest-neighbor interpolation method
We can also use the nearest neighbor interpolation to interpolate a matrix.
The MATLAB code to interpolate a matrix using the nearest neighbor interpolation algorithm is given below.
% Create a sample matrix to be interpolated mat = [5, 3, 9, 4, 2, 1, 5, 6, 7, 3, 4]; % Create indices for the input matrix mat_ind = 1:numel(mat); % Define new indices to increase resolution of the matrix new_ind = linspace(1, numel(mat), 30); % Perform nearest-neighbor interpolation of the matrix new_mat = interp1(mat_ind, mat, new_ind, 'nearest'); % Display the input matrix and new matrix disp('Input matrix:'); disp(mat); disp('Interpolated matrix:'); disp(new_mat); % Plot the matrices for visual comparison figure; subplot(2, 1, 1); stem(mat_ind, mat, 'o-', 'LineWidth', 1); title('Original Matrix'); xlabel('Index'); ylabel('Value'); subplot(2, 1, 2); stem(new_ind, new_mat, 'r-', 'LineWidth', 1); title('Nearest-Neighbor Interpolated Matrix'); xlabel('Index'); ylabel('Interpolated Value');
Output
It will produce the following ooutput
Input matrix: 5 3 9 4 2 1 5 6 7 3 4 Interpolated matrix: Columns 1 through 7 5 5 3 3 3 9 9 Columns 8 through 14 9 4 4 4 2 2 2 Columns 15 through 21 1 1 5 5 5 6 6 Columns 22 through 28 6 7 7 7 3 3 3 Columns 29 through 30 4 4
The visual representation of these two matrices is shown in the following figure.

Code Explanation
In this MATLAB example, firstly we create a sample matrix. Then, we create indices for this input matrix. After that we define new indices to interpolate the matrix to increase its resolution.
Next, we apply the nearest-neighbor interpolation algorithm using the "nearest" option within the "interp1" function to interpolate the matrix to new indices.
Finally, we display the original and interpolated matrices and their visual appearance.
Conclusion
In conclusion, the nearest neighbor interpolation algorithm is a simple and efficient method to interpolate a set of discrete data points. This interpolation method works by interpolating and assigning values of the nearest neighbor to the interpolation point.
This method is widely used in various applications such as in digital image processing to increase the image resolution, in signal processing for signal reconstruction, in geographic information system to estimate data points between locations, computer graphics, etc. However, there are some limitations of the nearest neighbor interpolation, such as it cannot be applied to continuous data or functions.
In this article, I have explained the nearest neighbor interpolation algorithm and its implementation in MATLAB programming. The examples included in this article demonstrate the practical implementation of nearest neighbor interpolation algorithm in MATLAB.