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

MathGroup Archive 2009

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

Search the Archive

Re: second simple problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99683] Re: second simple problem
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Mon, 11 May 2009 06:23:39 -0400 (EDT)
  • References: <gu666q$9tf$1@smc.vnet.net>

Hi,
> I have the following list:
> ex={1,5,7,4,"M",6,7,8,9,1,"M",3}
> I want to replace the M's in the following way: the first M by 5, and the second by2.
> Thus I have a replacement list
> rL={5,2}
> The problem is to get ={1,5,7,4,5,6,7,8,9,1,2,3}
> How can I do this in the most general form (for any length of ex and any number of values of "M")?
> Thanks
> Francisco      
> 
Here is one way:

res = ex;
MapThread[(res[[#1]] = #2) &, {Position[ex, "M"], rL}];
res

of course it will only work when Length[rL]==Length[Cases[ex,"M"]], if
not, you would need to pad or cut rL. If you don't need ex anymore, you
can of course operate directly on ex instead of the copy res.

hth,

albert


  • Prev by Date: Re: simple Question
  • Next by Date: Re: second simple problem
  • Previous by thread: Re: second simple problem
  • Next by thread: Re: second simple problem