Determining the range into which a number falls

Given a given number of ranges:

a =

    32225   52259
    52260   70794
    70795   91459
    91460   95409

And one value x = 61450- is there a way to determine in which range x falls without , using a loop to test every possibility? The answer in this case will be 2, since 61450 falls into the second range.

+4
source share
2 answers

using

res = find(x >= a(1,:) & x < a(2,:));
+9
source

OK, I did one thing :-).

foo = [1;round(1e5*rand(1000,1))];
foop = [ foo(2:end)+1;1e6];

x = 1e5*rand(1,1);
tic
for j = 1:1000

    bardro = find(x >= foo & x <= foop);
end
tocdro = toc;

tic;
for j = 1:1000
    barlui = sum(x >=foo);
end
toclui  = toc;

>> tocdro
tocdro =
    0.0113
>> toclui
toclui =
    0.0047

We have a winner!

+5
source

All Articles