找到数组中项目的值或最相似的值

时间:2014-11-24 04:47:19

标签: matlab

我正在尝试为数组中的项找到值或最相似的值。例如,如果Item为3且数组为[1,2,5,6,9],那么最相似的值为2,因为它具有最小的差异。我已经做到了,但我觉得有一种更有效的方法可以做到这一点,因为有时它会给出错误的价值观......任何想法?

我的代码:

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
相关问题