MathGroup Archive 2009

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

Search the Archive

Re: Replace specific element with specific value.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99633] Re: Replace specific element with specific value.
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Sun, 10 May 2009 05:13:19 -0400 (EDT)
  • References: <gu3av0$1f$1@smc.vnet.net>

A simple replacement can do this:

data /. {x -> x + xx, y -> y + yy, z -> z + zz}

if x,y or z may also be present in the first term and replacement
there is not desired:

data /. {{a_, x} -> {a, x + xx}, {a_, y} -> {a, y + yy}, {a_,
    z} -> {a, z + zz}}

Cheers -- Sjoerd

On May 9, 9:22 am, nick.... at mssm.edu wrote:
> I tried this code, it gives me right and wrong results spreeded into
> nested lists.
>
> data = {{0, 2}, {2, x}, {3, 4}, {4, 6}, {6, y}, {8, 9}, {9, z}}
>
> Table[{#1, #2 + Which[#2 === j, i, #2 =!= j, 0]} & @@@
>   data, {i, {xx, yy, zz}}, {j, {x, y, z}}]
>
> needed output:
>
> {0, 2}, {2, x+xx}, {3, 4}, {4, 6}, {6, y+yy}, {8, 9}, {9, z+zz}}
>
> While searching only the second element of each list, if x is found
> add xx and if y is found add yy to it etc.
>
> How to. Thanks in advance, Nick.



  • Prev by Date: Re: Clickable options
  • Next by Date: simple question
  • Previous by thread: Re: Replace specific element with specific value.
  • Next by thread: Re: Replace specific element with specific value.