http://www.mathworks.com/matlabcentral/newsreader/view_thread/321601
MATLAB Central Newsreader  seacrhing/indexing a vector
Feed for thread: seacrhing/indexing a vector
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 06 Jul 2012 12:23:07 +0000
seacrhing/indexing a vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321601#882084
Daniel Robbins
Hi,<br>
<br>
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.<br>
<br>
In a previous code I acheieved this with the line of code:<br>
<br>
[c index] = min(abs(gh)); <br>
<br>
However if I try:<br>
<br>
[c index] = x <br>
<br>
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?<br>
<br>
Thank you<br>
Dan

Fri, 06 Jul 2012 12:56:17 +0000
Re: seacrhing/indexing a vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321601#882087
dpb
On 7/6/2012 7:23 AM, Daniel Robbins wrote:<br>
> Hi,<br>
><br>
> I am trying to index a vector so I can establish the location of a given<br>
> number. So for example, if x = 5 10 15 20 25 I can search for 20 and be<br>
> given teh answer that 20 is the 4th number in the vector.<br>
<br>
If there is known to be an exact match or must be exact,<br>
NB that if values are floating point and non integral you may need to <br>
add a tolerance to the comparison rather than exact to find "close <br>
enough" values.<br>
<br>
loc=find(x==20);<br>
<br>
> In a previous code I acheieved this with the line of code:<br>
><br>
> [c index] = min(abs(gh));<br>
> However if I try:<br>
><br>
> [c index] = x<br>
> It does not work. Please can someone offer some help as to how I can<br>
> search along a vector for a specific value and identify the location of<br>
> the value?<br>
<br>
I don't know what you would expect the above to do...it's not a legal <br>
syntax, anyway.<br>
<br>
doc interp1 % will solve the above problem for you...<br>
<br>
>> x=[5:5:25]; y=1:length(x);<br>
>> interp1(x,y,23,'nearest')<br>
ans =<br>
5<br>
>> interp1(x,y,3,'nearest')<br>
ans =<br>
NaN<br>
>> interp1(x,y,7,'nearest')<br>
ans =<br>
1<br>
>><br>
<br>


Fri, 06 Jul 2012 12:59:22 +0000
Re: seacrhing/indexing a vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321601#882088
Nasser M. Abbasi
On 7/6/2012 7:23 AM, Daniel Robbins wrote:<br>
> Hi,<br>
><br>
> I am trying to index a vector so I can establish the location of a<br>
>given number. So for example, if x = 5 10 15 20 25 I can search<br>
>for 20 and be given teh answer that 20 is the 4th number in the vector.<br>
><br>
> In a previous code I acheieved this with the line of code:<br>
><br>
> [c index] = min(abs(gh));<br>
><br>
> However if I try:<br>
><br>
> [c index] = x<br>
><br>
> It does not work. Please can someone offer some help as to<br>
>how I can search along a vector for a specific value and identify the location of the value?<br>
><br>
> Thank you<br>
> Dan<br>
><br>
<br>
hi Dan;<br>
<br>
in the first case where it 'worked', you were<br>
making a call to what is called a 'function'<br>
<br>
min is a matlab 'function', not a variable like 'x'<br>
is in your second example.<br>
<br>
a 'function' can return more than one thing if it wants.<br>
if you see the help on min, you'll see it says<br>
<br>
" [Y,I] = min(X) returns the indices of the minimum values in vector I."<br>
<br>
but when you write, your second case:<br>
<br>
[c index] = x<br>
<br>
here 'x' is not a function. it is just a variable name,<br>
which happened to be an vector.<br>
<br>
To answer your main question, you can use find() function<br>
to "search along a vector for a specific value<br>
and identify the location of the value"<br>
<br>
help find and look at the examples there<br>
<br>
Nasser

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