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(gh));
> 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
>>

