check whether an interval instant belongs to an array of intervals

3 次查看(过去 30 天)
Hi everbod!
have an array of intervals like this C = {[1.54,2.49], [4.97,5.88], [6.69,7.98], [8.05,8.25],[8.58,9.09], [10.48,10.86], [11.16,12.97], [14.51,15.24],[15.38,15.60], [17.20,17.74], [19.16,22.38], [23.69,23.73],[24.59,25.47], [25.59,26.22], [26.65,29.27]};
I want to check if current intervals instant belongs to this array. Where interval divided into different parts e.g. 1.54 to 2.49 is the first part and so on..How can I make program for this?
for example
A=2;
whether or not belongs to 'C'? Help me please

采纳的回答

Kevin Hellemans
Kevin Hellemans 2020-4-14
Hi,
I would use cellfun to loop through the intervals and check in each cell. The function belows takes the cell array C and value A, checks if A is part of C and, returns true/false (tf) and if true, provides the position as optional output.
function [tf, position] = ValueInArray(C,A)
result = cellfun(@(x) inInterval(x,A),C);
tf = sum(result) > 0;
if tf
position = find(result);
else
position = [];
end
end
function tf = inInterval(interval,val)
lowerLimit = val >= min(interval);
upperLimit = val <= max(interval);
tf = lowerLimit && upperLimit;
end
With the values provided, this is my output:
[tf, position] = ValueInArray(C,A)
tf =
logical
1
position =
1
  1 个评论
Walter Roberson
Walter Roberson 2020-4-14
tf = sum(result) > 0;
if tf
position = find(result);
else
position = [];
end
can be simplified to
position = find(result);
provided that C is scalar. (If it is not scalar then your code would fail.)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by