How do I find values close to a given value?

前端 未结 6 1536
情歌与酒
情歌与酒 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:49

    if the data is sorted you can use find:

    i_lower  = find(data <= b,1,'last');
    i_higher = find(data >= b,1,'first');
    
    lower_than_b  = data(i_lower)
    higher_than_b = data(i_higher)
    

提交回复
热议问题