Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using ReplaceAll (/.) on numerical digits

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113283] Re: Using ReplaceAll (/.) on numerical digits
  • From: Thomas Dowling <thomasgdowling at gmail.com>
  • Date: Thu, 21 Oct 2010 07:05:39 -0400 (EDT)

Hello,

 data = 0.5600 + 1/10000 {20, 30, 40} {0.562, 0.563, 0.564}

FullForm[data]

identifies the problem.

You need to eliminate 0.5600.  For example:

data2 = With[{n = 10000}, N[(5600/n ) + 1/n {20, 30, 40}]]

FullForm@data2

data2 /. {0.562 -> 0.5622, 0.563 -> 0.5637, 0.564 -> 0.5641}

Out[5]= {0.5622, 0.5637, 0.5641}

On Wed, Oct 20, 2010 at 9:10 AM, Richard Klopp <rklopp at exponent.com> wrote:

> Background:
> I made some lab measurements and then calibrated the test instrument after
> the fact. This changed the measurements slightly. I wanted to replace the
> measurements with the calibrated values. The measurements were all very
> close to 0.5600, only differing in the thousandths and ten-thousandths
> places, so I coded my original data as a list of two-digit integers
> {20,30,40}, then divided the whole list by 10000 and added 0.5600 to it.
>
> I found some seemingly weird behavior with ReplaceAll as shown below. Note
> how 0.563 in data remains untouched while 0.562 and 0.564 get replaced as
> intended, but when I simply type in 0.563 and do a replace, it gets
> replaced
> successfully. Can someone explain why this happens?
>
> In any event, I've got a workaround based on StringReplace, which is likely
> safer whether the weird behavior is a bug or a misunderstanding on my part.
>
> data = 0.5600 + 1/10000 {20, 30, 40} {0.562, 0.563, 0.564} (* 0.563 stays
> untouched *) data /. {0.562 -> 0.5622, 0.563 -> 0.5637, 0.564 -> 0.5641}
> {0.5622, 0.563, 0.5641} (* 0.563 stays untouched *) (0.5600 + 1/10000 {20,
> 30, 40}) /. {0.562 -> 0.5622, 0.563 -> 0.5637,   0.564 -> 0.5641} {0.5622,
> 0.563, 0.5641} (* 0.563 stays untouched *) {0.5600 + 20/10000, 0.5600 +
> 30/10000,   0.5600 + 40/10000} /. {0.562 -> 0.5622, 0.563 -> 0.5637,
> 0.564
> -> 0.5641} {0.5622, 0.563, 0.5641} (* 0.563 gets replaced when typed in
> directly *) {0.562, 0.563, 0.564, 0.5630} /. {0.562 -> 0.5622, 0.563 ->
> 0.5637,   0.564 -> 0.5641} {0.5622, 0.5637, 0.5641, 0.5637}
>
>
> (*workaround*) replaceDigits[numList_List, replacementDigitsList_List] :=
> ToExpression[  StringReplace[   ToString[PaddedForm[#, {5, 5}] & /@
> numList],    Map[ToString[PaddedForm[#, {5, 5}]] &, replacementDigitsList,
> {2}]   ]  ] replaceDigits[data, {0.562 -> 0.5622, 0.563 -> 0.5637,   0.564
> -> 0.5641}] {0.5622, 0.5637, 0.5641}
>
>


  • Prev by Date: Re: Using ReplaceAll (/.) on numerical digits
  • Next by Date: Re: Using ReplaceAll (/.) on numerical digits
  • Previous by thread: Re: Using ReplaceAll (/.) on numerical digits
  • Next by thread: Re: Using ReplaceAll (/.) on numerical digits