簡體   English   中英

查找點B在Matlab中位於哪個間隔

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

我有一個間隔A的數組,我必須找到一個點B,它位於A中這些間隔之一之間。我無法通過A循環找到間隔。 例如:

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

假設間隔按升序排列,則可以使用注釋中指出的find(B < A, 1) - 1 如果B在整個范圍之外,則將返回一個空矩陣。 如果不希望這樣,您可以在之前添加檢查。

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