How do I find values close to a given value?

前端 未结 6 1552
情歌与酒
情歌与酒 2020-12-09 08:56

I have data = [1 1.2 1.3 1.5 1.8]

I want to find closest values before and after from data for this point, b = 1.23

How do I do th

6条回答
  •  误落风尘
    2020-12-09 09:59

    This method generalizes Doubt's answer to the case where there are multiple elements in b that you are searching for:

    ind=knnsearch(data',b) c=data(ind)

    which returns the index (or array of indices), ind, of the closest element (or elements) in data to the elements listed in b.

    Note that data is transposed because the set to be searched in needs to be a column vector. If be had multiple elements then it should also be a column vector.

    Also, this method can be generalized to give 2nd, 3rd, 4th ... closest neighbors (see documentation).

    It also generalizes to the case when the data is higher dimensional (If d dimensions then test and b would have d columns).

提交回复
热议问题