Home > Archive > Matlab > January 2008 > Finding nearest value in a vector
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Finding nearest value in a vector
|
|
| Peter Bone 2008-01-31, 8:28 pm |
| "John Chapman" <195827.remove@swan.ac.uk> wrote in message
<fnsv3v$3oe$1@fred.mathworks.com>...
> Hello,
> I have a vector of values and I wish to find the nearest
> vector element to a single variable (lookup nearest
> value). I have tried using interp1 with method = 'nearest'
> but as I do not have 2 vectors I am finding this does not
> work. Could anyone point me in the right direction? I'm
> sure this is fairly straight forward but cannot seem to
> find the right command.
>
> Thanks, John
>
Subtract your single variable from all elements in the array
and then abs all the elements. You're then looking for the
smallest value, which you can find using the min function.
Peter
| |
| John Chapman 2008-01-31, 8:28 pm |
| Hello,
I have a vector of values and I wish to find the nearest
vector element to a single variable (lookup nearest
value). I have tried using interp1 with method = 'nearest'
but as I do not have 2 vectors I am finding this does not
work. Could anyone point me in the right direction? I'm
sure this is fairly straight forward but cannot seem to
find the right command.
Thanks, John
|
|
|
|
|