简体   繁体   English

查找点B在Matlab中位于哪个间隔

[英]Find which interval a point B is located in Matlab

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. 我有一个间隔A的数组,我必须找到一个点B,它位于A中这些间隔之一之间。我无法通过A循环找到间隔。 For ex: 例如:

A = [1 3 4 6 10];


1 3
3 4
4 6
6 10

if B =2.3
returns 1

if B = 6.32
return 4

Assuming the intervals are in ascending order, you can use find(B < A, 1) - 1 as pointed out in the comments. 假设间隔按升序排列,则可以使用注释中指出的find(B < A, 1) - 1 This will return an empty matrix if B is outside the whole range. 如果B在整个范围之外,则将返回一个空矩阵。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM