check whether an interval instant belongs to an array of intervals

8 ビュー (過去 30 日間)
Muhammad Atif
Muhammad Atif 2020 年 4 月 14 日
コメント済み: Walter Roberson 2020 年 4 月 14 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by