Find the Closest Value in an Array in MATLAB


MATLAB is a programming environment developed for scientists and engineers to design and analyze system, perform data analysis, create visualizations, and more. MATLAB stands for Matrix Laboratory, it is a programming and interactive platform developed by MathWorks to provide a variety of tools for programming mathematical functions and operations, data analysis, etc. MATLAB is extensively used in different field of science, engineering, finance, economics, and more.

Read this tutorial to learn the different methods of finding the closest value in an array in MATLAB. But before that let's get an idea about Array in MATLAB.

What is an Array in MATLAB?

In MATLAB, a collection of elements of the same data type which are organized into one or more dimensions is referred to as an Array. Array is similar to a Matrix in which the elements are arranged in rows and columns.

  • In MATLAB, arrays can be of any dimensions, from one dimensional array to multi-dimensional array. In MATLAB, an array is a type fundamental data structure which is used to store and manipulate the data.

  • Depending on a certain application, a MATLAB array can be used to store numeric data, logical data, characters, or any other data types.

  • In MATLAB, an array is created by using the "square brackets '[]'", where the elements of the array are separated by comma symbols.

Examples of Array in MATLAB

The following code is an example of a one-dimensional array having seven elements −

A = [1, 2, 3, 4, 5, 6, 7]

Output

It will produce the following output

A =
1	2	3	4	5	6	7

The following code is an example of a multi-dimensional array

A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]

Output

It will produce the following output

A =
1	2	3	4
5	6	7	8
9	10	11	12

In MATLAB, array is one of the fundamental and widely used type of data structure which is used in a wide range of applications.

Now, let us discuss the different methods of finding the closest value in an array in MATLAB.

Finding the Closest Value in an Array in MATLAB

In this section of the tutorial, we will discuss different methods of finding the closest value to a given target value in an array in MATLAB.

Method 1 – Nearest Neighborhood Interpolation Method −

In MATLAB, we can use the nearest neighborhood interpolation method to find the closest value in a given array. To do this, a function "interp1()" is used. Then, we can perform interpolation by using 'nearest', i.e. nearest neighbor interpolation.

Syntax − The following code shows the syntax of "interp1()" function with nearest neighbor interpolation method in MATLAB,

Interp1(array, array, target, 'nearest')

Example 1

% Create an array
A = [1, 2, 3, 5, 7, 9]
% Select a target value
target = 1.4
% Use interp1() function and nearest neighbor interpolation to find the closest value
Closest_Value = interp1(A, A, target, 'nearest')

Output

It will produce the following output

A =
1	2	3	5	7	9
target = 1.4000
Closest_Value = 1

Explanation

In this program, first we have created an array "A" with values "[1, 2, 3, 5, 7, 9]". We then select a target value of "1.4" that we want to find the closest value to in the given array. After that we have used the "interp1()" function to find the closest value using the nearest neighbor interpolation.

In the "interp1()" function, the first argument specifies the array to interpolate from the array, here it is "A", the second argument specifies the respective output value, here it is also "A", the third argument is the target value which is "1.4", and the fourth argument specifies the method of interpolation, here it is "nearest".

Therefore, the output of this MATLAB program is the value "1" that is closest value to the target value of "1" in the given array "A".

Important − The method explain in the above program is applicable only when the target value is in between the maximum and minimum value of the array, if the target value is less than or greater than the maximum and minimum value in the array, then the above code will return NaN as output, refer the examples program given below.

Example 2

% Create an array
A = [1, 2, 3, 5, 7, 9]
% Select a target value
target = 12
% Use interp1() function and nearest neighbor interpolation to find the closest value
Closest_Value = interp1(A, A, target, 'nearest')

Output

It will produce the following output

A =
1	2	3	5	7	9
target = 12
Closest_Value = NaN

Example 3

% Create an array
A = [1, 2, 3, 5, 7, 9]
% Select a target value
target = -30
% Use interp1() function and nearest neighbor interpolation to find the closest value
Closest_Value = interp1(A, A, target, 'nearest')

Output

It will produce the following output

A =
1	2	3	5	7	9
target = -30
Closest_Value = NaN

To solve this issue, we use the following method.

Method 2 – Nearest Neighborhood Extrapolation Method −

In MATLAB, while finding closest value in an array, sometimes we have a target value which is either greater than or less than the maximum or minimum value of the array. In such case, the output of the interp1 function will be a NaN value as demonstrated in the above two programs.

To solve this issue, we use "nearest neighborhood extrapolation method", where the interp1() function is used with the "extrap" option. This "extrap" option allows us to find the closest value when the target value is outside the range of the array by extrapolating the value beyond the range of the array.

Syntax − The following code shows the syntax of "interp1()" function with nearest neighbor extrapolation method in MATLAB.

Interp1(array, array, target, 'nearest', 'extrap')

Now, let us understand the concept with the help of an example programs.

Example 4

% Create an array
A = [1, 2, 3, 5, 7, 9]
% Select a target value
target = -30
% Use interp1() function and nearest neighbor extrapolation to find the closest value
Closest_Value = interp1(A, A, target, 'nearest', 'extrap')

Output

It will produce the following output

A =
1	2	3	5	7	9
target = -30
Closest_Value = 1

Example 5

% Create an array
A = [1, 2, 3, 5, 7, 9]
% Select a target value
target = 12
% Use interp1() function and nearest neighbor extrapolation to find the closest value
Closest_Value = interp1(A, A, target, 'nearest', 'extrap')

Output

It will produce the following output

A =
1	2	3	5	7	9
target = 12
Closest_Value = 9

In both these programs, the "extrap" option tells MATLAB to extrapolate to find the closest value when the target value is outside the range of the given array "A".

Conclusion

In this tutorial, we explained in detail the different methods of finding the closest value in an array in MATLAB, with the help of some example programs.

Updated on: 26-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements