Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
check if numbers are within range

Subject: check if numbers are within range

From: pr

Date: 15 Aug, 2011 21:31:28

Message: 1 of 7

a = [3 9; 4 8; 2 6]
b = [1 10; 11 15; 7 9]

how do i the find indices of pairs in 'a' that fall between each pair in 'b'. answer:

for the first pair in 'b', [1 10], the indices in 'a' are 1, 2, and 3.
for the second pair in 'b', [2 5], none in 'a' are present.
for the third pair in 'b', [7 9], the indices in 'a' are 1 and 2.

do i need to create a vector for each pair in both arrays and then use ismember? or is there a more simple way to do this?

Subject: check if numbers are within range

From: ImageAnalyst

Date: 15 Aug, 2011 22:00:08

Message: 2 of 7

Before we give hints, are there cases where 1 of the pair is "in" and
1 is "out"? For example a pair 5, 1000. Do both have to be in?

Is this a homework assignment?

You wouldn't use ismember - that checks for members, not if some value
is in between two other values. Have you tried the obvious brute
force approach? Might not be as compact as some short cryptic ways
but at least you'll understand what you did.

Subject: check if numbers are within range

From: Pr B

Date: 16 Aug, 2011 00:14:28

Message: 3 of 7

No, this is not a homework problem. Just something I thought matlab would have a function for.

If you have the pair 5,1000 in 'b' it would be in 'a', specifically it would be in 1, 2, and 3. even if 'a' had one element present in 'b', it would be in.

As for a solution, the reason I thought to use ismember was the following:

I would check to see if the elements in 'a' were in 'b' in a loop, converting the pair in 'a' to a vector and the pair in 'b' to a vector and then using an ismember/intersect to check if there were any overlapping elements. This seems to be memory intensive. The other solution would be to do: find(a(:,1)&a(:,2)<b(:,1)|a(:,1)&a(:,2)>b(:,2) == 0)...does this look right? is there something more elegant for this that i'm not thinking of?

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0a25bd01-03d3-4bd0-a1f1-f7fe3c232c5e@l7g2000vbz.googlegroups.com>...
> Before we give hints, are there cases where 1 of the pair is "in" and
> 1 is "out"? For example a pair 5, 1000. Do both have to be in?
>
> Is this a homework assignment?
>
> You wouldn't use ismember - that checks for members, not if some value
> is in between two other values. Have you tried the obvious brute
> force approach? Might not be as compact as some short cryptic ways
> but at least you'll understand what you did.

Subject: check if numbers are within range

From: Pr B

Date: 16 Aug, 2011 01:14:28

Message: 4 of 7

more like: find((a(:,1)<b(1)&a(:,2)<b(1)|a(:,1)>b(2)&a(:,2)>b(2))==0)?

Subject: check if numbers are within range

From: Bruno Luong

Date: 16 Aug, 2011 05:44:13

Message: 5 of 7

"pr" wrote in message <j2c37g$40p$1@newscl01ah.mathworks.com>...
> a = [3 9; 4 8; 2 6]
> b = [1 10; 11 15; 7 9]
>
> how do i the find indices of pairs in 'a' that fall between each pair in 'b'. answer:
>
> for the first pair in 'b', [1 10], the indices in 'a' are 1, 2, and 3.

Fine

> for the second pair in 'b', [2 5], none in 'a' are present.

Where is [2 5]? I read [11 15].

> for the third pair in 'b', [7 9], the indices in 'a' are 1 and 2.

Why? 3.5 and 4.5 are not falling in [7 9].

Bruno

Subject: check if numbers are within range

From: Pr B

Date: 16 Aug, 2011 06:31:26

Message: 6 of 7

sorry, b = [1 10; 11 5; 7 9]; and none in 'a' are present. typo.

and i am also talking about integers only. furthermore, each pair in 'a' and 'b' represent the start and end of a range between the two values in the pair. i.e. [3 9] is the start and end of the range [3 4 5 6 7 8 9].

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j2d03d$oa7$1@newscl01ah.mathworks.com>...
> "pr" wrote in message <j2c37g$40p$1@newscl01ah.mathworks.com>...
> > a = [3 9; 4 8; 2 6]
> > b = [1 10; 11 15; 7 9]
> >
> > how do i the find indices of pairs in 'a' that fall between each pair in 'b'. answer:
> >
> > for the first pair in 'b', [1 10], the indices in 'a' are 1, 2, and 3.
>
> Fine
>
> > for the second pair in 'b', [2 5], none in 'a' are present.
>
> Where is [2 5]? I read [11 15].
>
> > for the third pair in 'b', [7 9], the indices in 'a' are 1 and 2.
>
> Why? 3.5 and 4.5 are not falling in [7 9].
>
> Bruno

Subject: check if numbers are within range

From: Bruno Luong

Date: 16 Aug, 2011 06:44:14

Message: 7 of 7

"Pr B" <pb2297@columbia.edu> wrote in message <j2d2ru$2he$1@newscl01ah.mathworks.com>...
> sorry, b = [1 10; 11 5; 7 9]; and none in 'a' are present. typo.
>
> and i am also talking about integers only. furthermore, each pair in 'a' and 'b' represent the start and end of a range between the two values in the pair. i.e. [3 9] is the start and end of the range [3 4 5 6 7 8 9].

Are you sure b(2,2) is 5 NOT 15?
b = [1 10; 11 15; 7 9];

Even only integers are considered, why {3,4,5} [ in a(:,1) interval] is in b(:,2) interval???

Can you please take a minimum care of describing the problem or make all necessary typo correction so we don't spend time to understand what you really want?

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us