MATLAB Answers

Check if a number is in between two values

985 views (last 30 days)
William Ferguson
William Ferguson on 29 Oct 2019
Commented: Walter Roberson on 29 Oct 2019
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 Comments

Rik
Rik on 29 Oct 2019
You mean like this?
tf= val>max(a) && val<min(b);
Should there be a check if a is indeed smaller than b?

Sign in to comment.

Answers (1)

Fabio Freschi
Fabio Freschi on 29 Oct 2019
Edited: Fabio Freschi on 29 Oct 2019
% 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 Comments

William Ferguson
William Ferguson on 29 Oct 2019
Sorry I forgot to include.
How would I count the number of satisfactory intervals?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!