MathGroup Archive 2001

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

Search the Archive

RE: Replacing 1 to many in a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29271] RE: [mg29250] Replacing 1 to many in a list
  • From: Mikael Adlers <mikael at mathcore.com>
  • Date: Sat, 9 Jun 2001 03:08:59 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,
the solution I found to the two problems was,

vector = {6, 6, 5, 4, 4, 4, Null, 6, Null, 44, Null, 4, 4, 5}

Module[{n = 1}, Map[# /. Null :> x[n++] &, vector]]

{6, 6, 5, 4, 4, 4, x[1], 6, x[2], 44, x[3], 4, 4, 5}

and

MapIndexed[#1 /. Null -> x[#2[[1]]] &, vector]

{6, 6, 5, 4, 4, 4, x[7], 6, x[9], 44, x[11], 4, 4, 5}

Hope it helps!

Regards,
/Mikael Adlers



------------------------------------------------------------------ 
 Mikael Adlers, Ph.D.          email: mikael at mathcore.com 
 MathCore AB                   phone: +4613 32 85 07 
 Wallenbergsgata 4             fax:         21 27 01
 SE-583 35 Linköping, Sweden   http://www.mathcore.com



> -----Original Message-----
> From: Chris Johnson [mailto:cjohnson at shell.faradic.net] 
To: mathgroup at smc.vnet.net
> Sent: den 8 juni 2001 10:16
> To: mathgroup at smc.vnet.net
> Subject: [mg29271] [mg29250] Replacing 1 to many in a list
> 
> 
> I think this is probably simple, but I can't find a natural 
> way to do it
> yet.  I hope someone out there can help.  I have a vector numbers with
> some Null's scattered throughout.  The number and location of 
> missing data
> varies.  For example,
> 
> vector = {6, 6, 5, 4, 4, 4, Null, 6, Null, 44, Null, 4, 4, 5}
> 
> Position[vector, Null]
> 
> {{7}, {9}, {11}}
> 
> Now "/." will let me replace all instances of Null with the 
> same variable
> but I want to replace each instance with a different 
> variable.  I would
> prefer to use x[1], x[2] and x[3], but if it were easier, 
> then x[7], x[9]
> and x[11] would be OK also.
> 
> The prefered end result would be:
> 
> {6, 6, 5, 4, 4, 4, x[1], 6, x[2], 44, x[3], 4, 4, 5}
> 
> Any suggestions?
> 
> Thanks in advance,
> Chris
> 
> 


  • Prev by Date: Re: Replacing 1 to many in a list
  • Next by Date: Normal vector on a surface
  • Previous by thread: Re: Replacing 1 to many in a list
  • Next by thread: Connecting Post RDBMS to Mathematica with J/Link