I am trying to find a value or the most similar value to an item in an array. As example if the Item is 3 and the array is [1,2,5,6,9] so the most similar value is 2 as it has the least difference. I already did it but I feel that there is a more efficient way to do it as sometimes it gives the values wrong .. any ideas?
My code:
value = 3;
array = [1 2 5 6 9];
cur = array - value;
theneededvalue = min(cur); %error as it gets the -ve value and I need the smallest positive value