check whether an interval instant belongs to an array of intervals
3 views (last 30 days)
Show older comments
Muhammad Atif
on 14 Apr 2020
Commented: Walter Roberson
on 14 Apr 2020
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
0 Comments
Accepted Answer
Kevin Hellemans
on 14 Apr 2020
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 Comment
Walter Roberson
on 14 Apr 2020
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.)
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!