Re: replace with closest value

• To: mathgroup at smc.vnet.net
• Subject: [mg119528] Re: replace with closest value
• From: DrMajorBob <btreat1 at austin.rr.com>
• Date: Wed, 8 Jun 2011 07:12:59 -0400 (EDT)
• References: <201106071045.GAA05887@smc.vnet.net>
• Reply-to: drmajorbob at yahoo.com

```a = {1.3, 1.5, 1.6, 1.8, 2.1, 2.3, 2.6, 2.9, 3.3, 3.7, 4.2, 4.7, 5.2,
5.9, 6.6, 7.4, 8.3, 9.4, 10.6, 11.8, 13.1, 14.8, 16.9, 19., 21.5,
23.6};
b = {1.2, 1.4, 1.6, 1.8, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 5,
6, 8, 10, 15, 20, 25, 40, 60, 100};
(Nearest[b] /@ a)[[All, 1]]

{1.4, 1.4, 1.6, 1.8, 2, 2.25, 2.5, 3, 3.25, 3.75, 4, 5, 5, 6, 6, 8, \
8, 10, 10, 10, 15, 15, 15, 20, 20, 25}

(Nearest[b] /@ a)[[All, -1]]

{1.4, 1.6, 1.6, 1.8, 2, 2.25, 2.5, 3, 3.25, 3.75, 4, 5, 5, 6, 6, 8, \
8, 10, 10, 10, 15, 15, 15, 20, 20, 25}

Mean /@ (Nearest[b] /@ a)

{1.4, 1.5, 1.6, 1.8, 2, 2.25, 2.5, 3, 3.25, 3.75, 4, 5, 5, 6, 6, 8, \
8, 10, 10, 10, 15, 15, 15, 20, 20, 25}

(Nearest[Rationalize@b] /@ Rationalize@a)[[All, 1]] /.
x_Rational :> N@x

{1.2, 1.4, 1.6, 1.8, 2, 2.25, 2.5, 3, 3.25, 3.75, 4, 5, 5, 6, 6, 8, \
8, 10, 10, 10, 15, 15, 15, 20, 20, 25}

eps = 10^-10;
-eps + Mean /@ (Nearest[b + eps] /@ a)

{1.2, 1.4, 1.6, 1.8, 2, 2.25, 2.5, 3, 3.25, 3.75, 4, 5, 5, 6, 6, 8, \
8, 10, 10, 10, 15, 15, 15, 20, 20, 25}

Mean /@ (Nearest[Rationalize@b] /@ Rationalize@a) /. x_Rational :> N@x

{1.3, 1.5, 1.6, 1.8, 2, 2.25, 2.5, 3, 3.25, 3.75, 4, 5, 5, 6, 6, 8, \
8, 10, 10, 10, 15, 15, 15, 20, 20, 25}

None of these are what you seem to expect, because of rounding.

Bobby

On Tue, 07 Jun 2011 05:45:51 -0500, graser <graser at gmail.com> wrote:

> Hi
>
> I have a some question for you!
>
> Let's say there is a list like that
> AA={1.3, 1.5, 1.6, 1.8, 2.1, 2.3, 2.6, 2.9, 3.3, 3.7, 4.2, 4.7, 5.2,
> 5.9, 6.6, 7.4, 8.3, 9.4, 10.6, 11.8, 13.1, 14.8, 16.9, 19., 21.5,
> 23.6}.
> Length[AA]=26
> I want to replace the element in the list with a closest number I
> specify in my list  BB,
> BB={1.2, 1.4, 1.6, 1.8, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 5,
> 6, 8 , 10 , 15, 20, 25, 40, 60, 100},
> Length[BB]=23
> so that the list AA after I replace will be
>
> AA={1.2, 1.5, 1.6, 1.8, 2. 2.25, 2.5,3.........20, 25}
> Length[AA]=26.
>
> I wonder if anyone can help me to solve this problem!
>
>

--
DrMajorBob at yahoo.com

```

• Prev by Date: Re: How to group data then apply functions to the grouped data?
• Next by Date: Re: replace with closest value
• Previous by thread: replace with closest value
• Next by thread: Re: replace with closest value