Check if a number is in between two values

675 ビュー (過去 30 日間)
William Ferguson
William Ferguson 2019 年 10 月 29 日
回答済み: Josep Llobet 2022 年 12 月 16 日
Say I have two column vectors:
a = [1,2,3,4,5,6]'
b = [10,11,12,13,14,15]'
Interval = [a b]
How would I check if a value like '8' is inbetween the defined interval?
Thank you
  2 件のコメント
Rik
Rik 2019 年 10 月 29 日
You mean like this?
tf= val>max(a) && val<min(b);
Should there be a check if a is indeed smaller than b?
William Ferguson
William Ferguson 2019 年 10 月 29 日
we can assume it will always be

サインインしてコメントする。

回答 (2 件)

Fabio Freschi
Fabio Freschi 2019 年 10 月 29 日
編集済み: Fabio Freschi 2019 年 10 月 29 日
% your data
a = [1,2,3,4,5,6]'
b = [10,11,12,13,14,15]'
Interval = [a b];
% number to check
x = 8;
% this finds the index of he rows(2) that have x in between
idx = find(x > Interval(:,1) & x < Interval(:,2));
% number of intervals with positive check
numIdx = sum(x > Interval(:,1) & x < Interval(:,2))
  3 件のコメント
Fabio Freschi
Fabio Freschi 2019 年 10 月 29 日
I edited the answer
Walter Roberson
Walter Roberson 2019 年 10 月 29 日
nnz(x >= a & x <= b)

サインインしてコメントする。


Josep Llobet
Josep Llobet 2022 年 12 月 16 日
function [min_proxim, max_proxim, porta_num_between] = app_sel_between_two(val_list, val_check)
% Detrmina si un numero (val_check) és dins d'una llista de números.
% Variables
% input
% val_list : list of values % ex: [1, 567 23];
% val_check : value to check % ex: 89
%
% output
% max_proxim : valor minim
% min_proxim : valor maxim
% porta_num_between : (true/false) porta si h iha numero entremig.
% INICI FUNCIÓ
val_list = sort(val_list);
% Més proper inferiors
max_proxim = max(val_list(find(val_check >= val_list)));
% Més proper superiors
min_proxim = min(val_list(find(val_check <= val_list)));
% Si hi ha algun valor:
if ~isempty(max_proxim) && ~isempty(min_proxim)
porta_num_between = true;
else
porta_num_between = false;
end
% FINAL FUNCIÓ
end

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by