Find which interval a point B is located in Matlab

后端 未结 1 1729
礼貌的吻别
礼貌的吻别 2020-12-22 09:26

I have an array of intervals A and I must find a point B that lays in between one of these intervals in A. I cannot loop thru A to find the interval. For ex:



        
相关标签:
1条回答
  • 2020-12-22 09:50

    Assuming the intervals are in ascending order, you can use find(B < A, 1) - 1 as pointed out in the comments. This will return an empty matrix if B is outside the whole range. If this is undesirable you could add in a check before.

    function interval = findInterval(A,B)
        if B > A(1) && B < A(end)
            interval = find(B < A, 1) - 1;
        else
            error('Interval is out of the range specified')
        end
    end
    
    0 讨论(0)
提交回复
热议问题