Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4605 > unrolled thread

With Nearest

Started bygraser <graser@gmail.com>
First post2011-08-27 12:19 +0000
Last post2011-08-30 00:01 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  With Nearest graser <graser@gmail.com> - 2011-08-27 12:19 +0000
    Re: With Nearest Yves Klett <yves.klett@googlemail.com> - 2011-08-28 08:08 +0000
    Re: With Nearest Peter Pein <petsie@dordos.net> - 2011-08-28 08:11 +0000
    Re: With Nearest Ray Koopman <koopman@sfu.ca> - 2011-08-29 07:48 +0000
    Re: With Nearest Daniel Lichtblau <danl@wolfram.com> - 2011-08-30 00:01 +0000

#4605 — With Nearest

Fromgraser <graser@gmail.com>
Date2011-08-27 12:19 +0000
SubjectWith Nearest
Message-ID<j3anc3$hdo$1@smc.vnet.net>
Dear Mathematica group,
I have a simple question for you.
Let's say there is an array or matrix like

AA = {{1.3, 1.5}, {21.5, 23.6}, {10, 19}};

If I want to extract a list whose second element is close to 20, How
can I do it with Nearest function?
Or is there any other method possible?
( In this case, the answer should be {10, 19} )

Thanks!

[toc] | [next] | [standalone]


#4627

FromYves Klett <yves.klett@googlemail.com>
Date2011-08-28 08:08 +0000
Message-ID<j3ct2g$5m3$1@smc.vnet.net>
In reply to#4605
Hi,

what about

AA = {{1.3, 1.5}, {21.5, 23.6}, {10, 19}};
Nearest[AA, 20, DistanceFunction -> (Norm[#2[[2]] - #1] &)]

which may be slow for large datasets.

Regards,
Yves

Am 27.08.2011 14:19, schrieb graser:
> AA = {{1.3, 1.5}, {21.5, 23.6}, {10, 19}};

[toc] | [prev] | [next] | [standalone]


#4633

FromPeter Pein <petsie@dordos.net>
Date2011-08-28 08:11 +0000
Message-ID<j3ct88$5n7$1@smc.vnet.net>
In reply to#4605
Am 27.08.2011 14:19, schrieb graser:
> Dear Mathematica group,
> I have a simple question for you.
> Let's say there is an array or matrix like
> 
> AA = {{1.3, 1.5}, {21.5, 23.6}, {10, 19}};
> 
> If I want to extract a list whose second element is close to 20, How
> can I do it with Nearest function?
> Or is there any other method possible?
> ( In this case, the answer should be {10, 19} )
> 
> Thanks!
> 
> 

Hi,

using Nearest, the following seems to be the fastest which comes to my mind:

In[4]:= SeedRandom[123];
        AA = 0.125*RandomInteger[320, {10^5, 2}];
        Timing[Length[ne =
          Parallelize[Cases[AA,
            {_, Alternatives @@ Nearest[AA[[All,2]], 20]}, 1]]
        ]]
Out[6]= {1.779,298}

Peter

[toc] | [prev] | [next] | [standalone]


#4643

FromRay Koopman <koopman@sfu.ca>
Date2011-08-29 07:48 +0000
Message-ID<j3fg87$fb1$1@smc.vnet.net>
In reply to#4605
On Aug 27, 5:19 am, graser <gra...@gmail.com> wrote:
> Dear Mathematica group,
> I have a simple question for you.
> Let's say there is an array or matrix like
>
> AA = {{1.3, 1.5}, {21.5, 23.6}, {10, 19}};
>
> If I want to extract a list whose second element is close to 20,
> How can I do it with Nearest function?
> Or is there any other method possible?
> ( In this case, the answer should be {10, 19} )
>
> Thanks!

If AA is long then

   AA[[ Ordering[ Abs[ AA[[All,2]] - 20 ], 1] [[1]] ]]
or
   Extract[ AA, Ordering[ Abs[ AA[[All,2]] - 20 ], 1] ]

will be much faster than anything using Nearest.

[toc] | [prev] | [next] | [standalone]


#4653

FromDaniel Lichtblau <danl@wolfram.com>
Date2011-08-30 00:01 +0000
Message-ID<j3h999$nht$1@smc.vnet.net>
In reply to#4605
On 08/29/2011 02:47 AM, Ray Koopman wrote:
> On Aug 27, 5:19 am, graser<gra...@gmail.com>  wrote:
>> Dear Mathematica group,
>> I have a simple question for you.
>> Let's say there is an array or matrix like
>>
>> AA = {{1.3, 1.5}, {21.5, 23.6}, {10, 19}};
>>
>> If I want to extract a list whose second element is close to 20,
>> How can I do it with Nearest function?
>> Or is there any other method possible?
>> ( In this case, the answer should be {10, 19} )
>>
>> Thanks!
>
> If AA is long then
>
>     AA[[ Ordering[ Abs[ AA[[All,2]] - 20 ], 1] [[1]] ]]
> or
>     Extract[ AA, Ordering[ Abs[ AA[[All,2]] - 20 ], 1] ]
>
> will be much faster than anything using Nearest.

That's quite true.

In[6]:= aa = RandomReal[40, {10^5,2}];

In[7]:= Timing[func = Nearest[Map[#[[2]]->#&, aa]];]
Out[7]= {0.17, Null}

In[8]:= Timing[aa[[ Ordering[ Abs[ aa[[All,2]] - 20 ], 1] [[1]] ]]]
Out[8]= {0.01, {23.7973, 19.9997}}

In[9]:= Timing[func[20]]
Out[9]= {0., {{23.7973, 19.9997}}}

But the Nearest approach does have some added flexibility in that it can 
be reused for values other than 20. Whether this is important would 
depend on the application, of course.

Daniel Lichtblau
Wolfram Research

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web