查找点B在Matlab中位于哪个间隔 [英] Find which interval a point B is located in Matlab
本文介绍了查找点B在Matlab中位于哪个间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个间隔A的数组,我必须找到一个点B,该点位于A中的这些间隔之一之间.我无法通过A循环查找间隔. 例如:
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:
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
超出整个范围,则将返回一个空矩阵.如果不希望这样,您可以在之前添加检查.
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
这篇关于查找点B在Matlab中位于哪个间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文