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:
how do you find adjacent numbers fast?

Subject: how do you find adjacent numbers fast?

From: Luna Moon

Date: 23 Jun, 2010 19:43:09

Message: 1 of 6

Here is the number I want to find, let's call it x,

here is the big vector, let me call it A,

I want to find the number x in A, in a fuzzy way.

Lets do an example: x=3, A=[0, 1, 2, 3, 4, 5],
then I want to identify the target location to be position 4.

Another example:
x=3, A=[0, 2, 4, 6, 8],
then I want to identify the target locations to be positions 2 and 3,
(i.e. the immediate location right before the placehold that x should
be in, and one immediately after...)
i.e. x=3 should have been in between 2 and 3, since we couldn't find
it in the vector

A is an ordered vector... not a set...

How to do that?

thanks

Subject: how do you find adjacent numbers fast?

From: John D'Errico

Date: 23 Jun, 2010 19:52:21

Message: 2 of 6

Luna Moon <lunamoonmoon@gmail.com> wrote in message <57dc7fe3-2b8b-4ed7-870c-b73c87ae24d4@a3g2000vbl.googlegroups.com>...
> Here is the number I want to find, let's call it x,
>
> here is the big vector, let me call it A,
>
> I want to find the number x in A, in a fuzzy way.
>
> Lets do an example: x=3, A=[0, 1, 2, 3, 4, 5],
> then I want to identify the target location to be position 4.
>
> Another example:
> x=3, A=[0, 2, 4, 6, 8],
> then I want to identify the target locations to be positions 2 and 3,
> (i.e. the immediate location right before the placehold that x should
> be in, and one immediately after...)
> i.e. x=3 should have been in between 2 and 3, since we couldn't find
> it in the vector
>
> A is an ordered vector... not a set...
>
> How to do that?

You won't believe me. People never do on this matter.
But the second return argument from histc does exactly
this.

HTH,
John

Subject: how do you find adjacent numbers fast?

From: Roger Stafford

Date: 23 Jun, 2010 19:57:04

Message: 3 of 6

Luna Moon <lunamoonmoon@gmail.com> wrote in message <57dc7fe3-2b8b-4ed7-870c-b73c87ae24d4@a3g2000vbl.googlegroups.com>...
> Here is the number I want to find, let's call it x,
>
> here is the big vector, let me call it A,
>
> I want to find the number x in A, in a fuzzy way.
>
> Lets do an example: x=3, A=[0, 1, 2, 3, 4, 5],
> then I want to identify the target location to be position 4.
>
> Another example:
> x=3, A=[0, 2, 4, 6, 8],
> then I want to identify the target locations to be positions 2 and 3,
> (i.e. the immediate location right before the placehold that x should
> be in, and one immediately after...)
> i.e. x=3 should have been in between 2 and 3, since we couldn't find
> it in the vector
>
> A is an ordered vector... not a set...
>
> How to do that?
>
> thanks

 [ignore,bin] = histc(x,A);

Roger Stafford

Subject: how do you find adjacent numbers fast?

From: Luna Moon

Date: 23 Jun, 2010 20:12:54

Message: 4 of 6

On Jun 23, 3:57 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Luna Moon <lunamoonm...@gmail.com> wrote in message <57dc7fe3-2b8b-4ed7-870c-b73c87ae2...@a3g2000vbl.googlegroups.com>...
> > Here is the number I want to find, let's call it x,
>
> > here is the big vector, let me call it A,
>
> > I want to find the number x in A, in a fuzzy way.
>
> > Lets do an example: x=3, A=[0, 1, 2, 3, 4, 5],
> > then I want to identify the target location to be position 4.
>
> > Another example:
> > x=3, A=[0, 2, 4, 6, 8],
> > then I want to identify the target locations to be positions 2 and 3,
> > (i.e. the immediate location right before the placehold that x should
> > be in, and one immediately after...)
> > i.e. x=3 should have been in between 2 and 3, since we couldn't find
> > it in the vector
>
> > A is an ordered vector... not a set...
>
> > How to do that?
>
> > thanks
>
>  [ignore,bin] = histc(x,A);
>
> Roger Stafford- Hide quoted text -
>
> - Show quoted text -


Great idea but ...

Slightly off... I have to use one more "if" to fix it...

>> x=3, A=[0, 1, 2, 3, 4, 5], [ignore,bin] = histc(x,A)

x =

     3


A =

     0 1 2 3 4 5


ignore =

     0 0 0 1 0 0


bin =

     4

>> x=3, A=[0, 1, 2, 4, 5], [ignore,bin] = histc(x,A)

x =

     3


A =

     0 1 2 4 5


ignore =

     0 0 1 0 0


bin =

     3

Subject: how do you find adjacent numbers fast?

From: John D'Errico

Date: 23 Jun, 2010 20:28:04

Message: 5 of 6

Luna Moon <lunamoonmoon@gmail.com> wrote in message <59fedfc4-9c35-400f-86ed-a7acbc9bc75b@20g2000vbi.googlegroups.com>...
> On Jun 23, 3:57 pm, "Roger Stafford"
> <ellieandrogerxy...@mindspring.com.invalid> wrote:
> > Luna Moon <lunamoonm...@gmail.com> wrote in message <57dc7fe3-2b8b-4ed7-870c-b73c87ae2...@a3g2000vbl.googlegroups.com>...
> > > Here is the number I want to find, let's call it x,
> >
> > > here is the big vector, let me call it A,
> >
> > > I want to find the number x in A, in a fuzzy way.
> >
> > > Lets do an example: x=3, A=[0, 1, 2, 3, 4, 5],
> > > then I want to identify the target location to be position 4.
> >
> > > Another example:
> > > x=3, A=[0, 2, 4, 6, 8],
> > > then I want to identify the target locations to be positions 2 and 3,
> > > (i.e. the immediate location right before the placehold that x should
> > > be in, and one immediately after...)
> > > i.e. x=3 should have been in between 2 and 3, since we couldn't find
> > > it in the vector
> >
> > > A is an ordered vector... not a set...
> >
> > > How to do that?
> >
> > > thanks
> >
> >  [ignore,bin] = histc(x,A);
> >
> > Roger Stafford- Hide quoted text -
> >
> > - Show quoted text -
>
>
> Great idea but ...
>
> Slightly off... I have to use one more "if" to fix it...
>
> >> x=3, A=[0, 1, 2, 3, 4, 5], [ignore,bin] = histc(x,A)
>
> x =
>
> 3
>
>
> A =
>
> 0 1 2 3 4 5
>
>
> ignore =
>
> 0 0 0 1 0 0
>
>
> bin =
>
> 4
>
> >> x=3, A=[0, 1, 2, 4, 5], [ignore,bin] = histc(x,A)
>
> x =
>
> 3
>
>
> A =
>
> 0 1 2 4 5
>
>
> ignore =
>
> 0 0 1 0 0
>
>
> bin =
>
> 3

So, pray tell, how is that NOT what you asked for?

bin is the number of the bin that x falls in. EXACTLY
so.

If you want to know the lower end point of that bin,
then what does A(bin) give you?

John

Subject: how do you find adjacent numbers fast?

From: Luna Moon

Date: 23 Jun, 2010 20:30:44

Message: 6 of 6

On Jun 23, 4:28 pm, "John D'Errico" <woodch...@rochester.rr.com>
wrote:
> Luna Moon <lunamoonm...@gmail.com> wrote in message <59fedfc4-9c35-400f-86ed-a7acbc9bc...@20g2000vbi.googlegroups.com>...
> > On Jun 23, 3:57 pm, "Roger Stafford"
> > <ellieandrogerxy...@mindspring.com.invalid> wrote:
> > > Luna Moon <lunamoonm...@gmail.com> wrote in message <57dc7fe3-2b8b-4ed7-870c-b73c87ae2...@a3g2000vbl.googlegroups.com>...
> > > > Here is the number I want to find, let's call it x,
>
> > > > here is the big vector, let me call it A,
>
> > > > I want to find the number x in A, in a fuzzy way.
>
> > > > Lets do an example: x=3, A=[0, 1, 2, 3, 4, 5],
> > > > then I want to identify the target location to be position 4.
>
> > > > Another example:
> > > > x=3, A=[0, 2, 4, 6, 8],
> > > > then I want to identify the target locations to be positions 2 and 3,
> > > > (i.e. the immediate location right before the placehold that x should
> > > > be in, and one immediately after...)
> > > > i.e. x=3 should have been in between 2 and 3, since we couldn't find
> > > > it in the vector
>
> > > > A is an ordered vector... not a set...
>
> > > > How to do that?
>
> > > > thanks
>
> > >  [ignore,bin] = histc(x,A);
>
> > > Roger Stafford- Hide quoted text -
>
> > > - Show quoted text -
>
> > Great idea but ...
>
> > Slightly off... I have to use one more "if" to fix it...
>
> > >> x=3, A=[0, 1, 2, 3, 4, 5],  [ignore,bin] = histc(x,A)
>
> > x =
>
> >      3
>
> > A =
>
> >      0     1     2     3     4     5
>
> > ignore =
>
> >      0     0     0     1     0     0
>
> > bin =
>
> >      4
>
> > >> x=3, A=[0, 1, 2, 4, 5],  [ignore,bin] = histc(x,A)
>
> > x =
>
> >      3
>
> > A =
>
> >      0     1     2     4     5
>
> > ignore =
>
> >      0     0     1     0     0
>
> > bin =
>
> >      3
>
> So, pray tell, how is that NOT what you asked for?
>
> bin is the number of the bin that x falls in. EXACTLY
> so.
>
> If you want to know the lower end point of that bin,
> then what does A(bin) give you?
>
> John- Hide quoted text -
>
> - Show quoted text -

when x is not in A, need two numbers (one before and one after)
returned...

it's no problem - i just have to add one more "if" statement to handle
that

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