Wednesday, September 14, 2016

Matlab: Finding the number in an array nearest to a given number

We can use the following method in matlab to find a value in an array which is nearest to a given number.

Let us say we have an array "temp" with the following numbers



Now if we want to find the number closest to 32 in the array we can do the following.



Subtract the given number from all the number in the array



find the absolute value of the difference by using absolute



Now find the minimum number in the diff array by using the function min



The variable difference will have the minimum difference and index will point to the index of the number.

temp(index) will give us the actual number closest to the given variable.


No comments:

Post a Comment

Follow by Email