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:
seacrhing/indexing a vector

Subject: seacrhing/indexing a vector

From: Daniel Robbins

Date: 6 Jul, 2012 12:23:07

Message: 1 of 4

Hi,

I am trying to index a vector so I can establish the location of a given number. So for example, if x = 5 10 15 20 25 I can search for 20 and be given teh answer that 20 is the 4th number in the vector.

In a previous code I acheieved this with the line of code:

[c index] = min(abs(g-h));

However if I try:

[c index] = x

It does not work. Please can someone offer some help as to how I can search along a vector for a specific value and identify the location of the value?

Thank you
Dan

Subject: seacrhing/indexing a vector

From: dpb

Date: 6 Jul, 2012 12:56:17

Message: 2 of 4

On 7/6/2012 7:23 AM, Daniel Robbins wrote:
> Hi,
>
> I am trying to index a vector so I can establish the location of a given
> number. So for example, if x = 5 10 15 20 25 I can search for 20 and be
> given teh answer that 20 is the 4th number in the vector.

If there is known to be an exact match or must be exact,
NB that if values are floating point and non integral you may need to
add a tolerance to the comparison rather than exact to find "close
enough" values.

loc=find(x==20);

> In a previous code I acheieved this with the line of code:
>
> [c index] = min(abs(g-h));
> However if I try:
>
> [c index] = x
> It does not work. Please can someone offer some help as to how I can
> search along a vector for a specific value and identify the location of
> the value?

I don't know what you would expect the above to do...it's not a legal
syntax, anyway.

doc interp1 % will solve the above problem for you...

 >> x=[5:5:25]; y=1:length(x);
 >> interp1(x,y,23,'nearest')
ans =
      5
 >> interp1(x,y,3,'nearest')
ans =
    NaN
 >> interp1(x,y,7,'nearest')
ans =
      1
 >>

--

Subject: seacrhing/indexing a vector

From: Nasser M. Abbasi

Date: 6 Jul, 2012 12:59:22

Message: 3 of 4

On 7/6/2012 7:23 AM, Daniel Robbins wrote:
> Hi,
>
> I am trying to index a vector so I can establish the location of a
>given number. So for example, if x = 5 10 15 20 25 I can search
>for 20 and be given teh answer that 20 is the 4th number in the vector.
>
> In a previous code I acheieved this with the line of code:
>
> [c index] = min(abs(g-h));
>
> However if I try:
>
> [c index] = x
>
> It does not work. Please can someone offer some help as to
>how I can search along a vector for a specific value and identify the location of the value?
>
> Thank you
> Dan
>

hi Dan;

in the first case where it 'worked', you were
making a call to what is called a 'function'

min is a matlab 'function', not a variable like 'x'
is in your second example.

a 'function' can return more than one thing if it wants.
if you see the help on min, you'll see it says

" [Y,I] = min(X) returns the indices of the minimum values in vector I."

but when you write, your second case:

         [c index] = x

here 'x' is not a function. it is just a variable name,
which happened to be an vector.

To answer your main question, you can use find() function
to "search along a vector for a specific value
and identify the location of the value"

help find and look at the examples there

--Nasser

Subject: seacrhing/indexing a vector

From: Daniel Robbins

Date: 6 Jul, 2012 13:25:08

Message: 4 of 4

Thanks gents, appreciate the feedback. I can see how to solve my problem now.

Thanks again.

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jt6nfb$9tt$1@speranza.aioe.org>...
> On 7/6/2012 7:23 AM, Daniel Robbins wrote:
> > Hi,
> >
> > I am trying to index a vector so I can establish the location of a
> >given number. So for example, if x = 5 10 15 20 25 I can search
> >for 20 and be given teh answer that 20 is the 4th number in the vector.
> >
> > In a previous code I acheieved this with the line of code:
> >
> > [c index] = min(abs(g-h));
> >
> > However if I try:
> >
> > [c index] = x
> >
> > It does not work. Please can someone offer some help as to
> >how I can search along a vector for a specific value and identify the location of the value?
> >
> > Thank you
> > Dan
> >
>
> hi Dan;
>
> in the first case where it 'worked', you were
> making a call to what is called a 'function'
>
> min is a matlab 'function', not a variable like 'x'
> is in your second example.
>
> a 'function' can return more than one thing if it wants.
> if you see the help on min, you'll see it says
>
> " [Y,I] = min(X) returns the indices of the minimum values in vector I."
>
> but when you write, your second case:
>
> [c index] = x
>
> here 'x' is not a function. it is just a variable name,
> which happened to be an vector.
>
> To answer your main question, you can use find() function
> to "search along a vector for a specific value
> and identify the location of the value"
>
> help find and look at the examples there
>
> --Nasser
>

Tags for 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