MATLAB Find Closest Value in Array Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report MATLAB arrays store numbers and allow users to perform various operations on them. In this article, we shall see how to find the closest value to a given target value in a MATLAB array. The same task can be performed by using the nearest neighbor interpolation, which we shall explore in the following sections with examples. Method 1: Using the Nearest Neighborhood Interpolation Using the nearest neighborhood interpolation method in MATLAB, we can find the value of the closest value to a given value in an array. This is done by using the interp1() function and selecting the interpolation as 'nearest'. interp1(array, array, <target value>, 'nearest') Example 1: Matlab % MATLAB code % using the interp1 function to get closest value % array arr=[1 2 3 4 5 6 7]; target = 2.3; %target value closest = interp1(arr,arr,target,'nearest') Output: This should return 2 as it is the closest value to 2.3 Method 2: Target Value is Greater or Less than Maximum and Minimum Value of ArrayIn this case, the standard interpolation method used above will return NaN as the queried values do not lie between the elements of the array. Example 2: Matlab % MATLAB code When target value is greater % or less than the maximum and minimum value of array arr=[1 2 3 4 5 6 7]; % Target lesser than minimum of array target1 = -31; % Target greater than maximum of array target2 = 23; closest_1 = interp1(arr,arr,target1,'nearest'); closest_2 = interp1(arr,arr,target2,'nearest'); Output: Both the interpolation results will return NaN. To fix this problem we only need to extrapolate on the given array which can be done by adding the 'extrap' parameter for extrapolation. See the following example for seeing the implementation of the same. Example 3: Matlab % MATLAB code for 'extrap' parameter arr=[1 2 3 4 5 6 7]; target = 31; closest = interp1(arr,arr,target,'nearest','extrap'); Output: Comment More infoAdvertise with us Next Article MATLAB Find Exact String in Cell Array O owl0223 Follow Improve Article Tags : Software Engineering Technical Scripter 2022 MATLAB Array-Programs Similar Reads MATLAB Find Exact String in Cell Array Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions, 2 min read How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array.  In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi 4 min read Find the Target number in an Array Finding a number within an array is an operation, in the field of computer science and data analysis. In this article, we will discuss the steps involved and analyze their time and space complexities. Examples: Input: Array: {10, 20, 30, 40, 50} , Target: 30Output: "Target found at index 2" Input: A 13 min read Find() function in MATLAB The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t 3 min read How to detect duplicate values and its indices within an array in MATLAB? In this article, we will discuss how to find duplicate values and their indices within an array in MATLAB. It can be done using unique(), length(), setdiff(), and numel() functions that are illustrated below: Using Unique() Unique(A) function is used to return the same data as in the specified array 3 min read Find the nearest value and the index of NumPy Array In this article, let's discuss finding the nearest value and the index in an array with Numpy. We will make use of two of the functions provided by the NumPy library to calculate the nearest value and the index in the array. Those two functions are numpy.abs() and numpy.argmin(). Example Input Arra 3 min read Like