finding the closest number in an array

  • Follow


I have a vector that looks something like this:
f= [ . . . . 1990 1998 2001 2004 . . . .]

and I would like to find the closest value to f=2000 ( which in this case could be 1998 or 2001 ) and the index (i)  of this value in vector f ( f(i) )

How can I do that ?

Thanks
0
Reply Shaddy 12/11/2009 8:39:04 PM

On Dec 11, 12:39=A0pm, "Shaddy " <shadd...@gmail.com> wrote:
> I have a vector that looks something like this:
> f=3D [ . . . . 1990 1998 2001 2004 . . . .]
>
> and I would like to find the closest value to f=3D2000 ( which in this ca=
se could be 1998 or 2001 ) and the index (i) =A0of this value in vector f (=
 f(i) )
>
> How can I do that ?
>
> Thanks

f=3D [1990 1998 2001 2004]
val =3D 2000 %value to find
tmp =3D abs(f-val)
[idx idx] =3D min(tmp) %index of closest value
closest =3D f(idx) %closest value
%%%%%%%%%%%%%%%%%%%
tmp =3D
    10     2     1     4
idx =3D
     3
closest =3D
        2001

Note: 2001 is closer to 2000 than 1998, unless you have a different
rule of "closeness".

-Nathan
0
Reply ngreco32 (530) 12/11/2009 8:51:25 PM


On Dec 11, 3:39=A0pm, "Shaddy " <shadd...@gmail.com> wrote:
> I have a vector that looks something like this:
> f=3D [ . . . . 1990 1998 2001 2004 . . . .]
>
> and I would like to find the closest value to f=3D2000 ( which in this ca=
se could be 1998 or 2001 ) and the index (i) =A0of this value in vector f (=
 f(i) )
>
> How can I do that ?
>
> Thanks

------------------------------------------------------------------
Use the min function:

f=3D [1990 1998 2001 2004 2001 33333]
[c index] =3D min(abs(f-2000))
closestValues =3D f(index) % Finds first one only!
0
Reply imageanalyst (7590) 12/11/2009 8:56:27 PM

That is great. Thank you

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <91317dda-d8e8-4da7-984f-08298a478511@g12g2000yqa.googlegroups.com>...
> On Dec 11, 3:39?pm, "Shaddy " <shadd...@gmail.com> wrote:
> > I have a vector that looks something like this:
> > f= [ . . . . 1990 1998 2001 2004 . . . .]
> >
> > and I would like to find the closest value to f=2000 ( which in this case could be 1998 or 2001 ) and the index (i) ?of this value in vector f ( f(i) )
> >
> > How can I do that ?
> >
> > Thanks
> 
> ------------------------------------------------------------------
> Use the min function:
> 
> f= [1990 1998 2001 2004 2001 33333]
> [c index] = min(abs(f-2000))
> closestValues = f(index) % Finds first one only!
0
Reply Shaddy 12/11/2009 9:03:22 PM

"Shaddy " <shaddyab@gmail.com> wrote in message <hfual8$rih$1@fred.mathworks.com>...
=================


Although, if f consists of monotonically increasing values, it can be faster to use histc(). See the following comparison:

f= 0:3:3e7;
val = 2000; %value to find

tic;

[N,bin]=histc(val,f);




    index=bin+1;
    if abs(val-f(bin))<abs(val-f(bin+1))

        fclosest=f(bin)
        index=bin;
    else
        fclosest=f(index)

    end


toc;
%Elapsed time is 0.017697 seconds.


tic

   tmp = abs(f-val);
  [idx idx] = min(tmp); %index of closest value
  closest = f(idx); %closest value


toc
%Elapsed time is 0.100856 seconds.


isequal(closest,fclosest); %1
isequal(idx,index); %1
0
Reply Matt 12/11/2009 9:19:20 PM

> f= [1990 1998 2001 2004 2001 33333]
> [c index] = min(abs(f-2000))
> closestValues = f(index) % Finds first one only!


What if I need the closest entry of the array but within limits?
Example to clarify:
I have the array test=[5,6,7,8,9,10] and I want the index of the entry closest to 5 out of the entries that are >=6 (therefore the index of 6 -> 2).
[~,idx]=min(abs(5-test)) returns 1 of course…
At first I though about trying something like
[~,idx]=min(abs(5-test(test>=6)))
but it won’t do since 6 has idx=1 within the test(test>=6) array and therefore the ans is still idx=1.

Any ideas?
Thanx in advance
0
Reply parisiwa (1) 1/22/2012 7:36:28 PM

On 1/22/2012 1:36 PM, Paris Papadopoulos wrote:
....

> What if I need the closest entry of the array but within limits?
> Example to clarify:
> I have the array test=[5,6,7,8,9,10] and I want the index of the entry
> closest to 5 out of the entries that are >=6 (therefore the index of 6
> -> 2).
> [~,idx]=min(abs(5-test)) returns 1 of course…
> At first I though about trying something like
> [~,idx]=min(abs(5-test(test>=6)))
> but it won’t do since 6 has idx=1 within the test(test>=6) array and
> therefore the ans is still idx=1.
>
> Any ideas?

Probably simplest to code is to simply make the restricted test array 
another variable and use it.  Then the returned index is the correct one 
directly.  Otherwise, create the indirect index into the array; I didn't 
take the time to see if it can be done w/o an intermediate step or not; 
I'm guessing not (at least w/o duplicating the logical test at least the 
second time).

--
0
Reply none1568 (6654) 1/23/2012 2:28:44 PM

---------------------------------------------------------------------------------------------------------
Hello all, i have one problem, if someone helps me, i will really appreciate. So please, HELP ME ! 
I have one array, let's say it is is like this:
A=[3, 5, 4, 6.2, 7.1, 7.8, 9.3, 10, 11, 13]
I have to find the closest value of 7 (<=7). And then, i have to search in this vector for all values that are smaller than the closest value that we found (from the value that we found in descending order one by one search till the first value of array), and to see that which of these values gives as maximum value of function f(i), the function is like below:

f(i) = 5/i+i/6+7/i

So, i have to test every value that we found (below the closest value) in the function below, to see which of these values gives us the highest value of f(i). 

PLEASEEEEE HELP ME, I WILL really appreciate

Thanks in advance
Regards
Luki

------------------------------------------------------------------------------------------------







dpb <none@non.net> wrote in message <jfjqqu$cva$1@speranza.aioe.org>...
> On 1/22/2012 1:36 PM, Paris Papadopoulos wrote:
> ...
> 
> > What if I need the closest entry of the array but within limits?
> > Example to clarify:
> > I have the array test=[5,6,7,8,9,10] and I want the index of the entry
> > closest to 5 out of the entries that are >=6 (therefore the index of 6
> > -> 2).
> > [~,idx]=min(abs(5-test)) returns 1 of course…
> > At first I though about trying something like
> > [~,idx]=min(abs(5-test(test>=6)))
> > but it won’t do since 6 has idx=1 within the test(test>=6) array and
> > therefore the ans is still idx=1.
> >
> > Any ideas?
> 
> Probably simplest to code is to simply make the restricted test array 
> another variable and use it.  Then the returned index is the correct one 
> directly.  Otherwise, create the indirect index into the array; I didn't 
> take the time to see if it can be done w/o an intermediate step or not; 
> I'm guessing not (at least w/o duplicating the logical test at least the 
> second time).
> 
> --
0
Reply LUAN.HAKAJ (6) 6/22/2012 1:23:07 AM

7 Replies
1159 Views

(page loaded in 1.84 seconds)

Similiar Articles:













7/23/2012 8:38:51 PM


Reply: