MathGroup Archive 2005

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

Search the Archive

Re: Elegant syntax for multiple conditional assignment?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54426] Re: [mg54337] Elegant syntax for multiple conditional assignment?
  • From: Scott Hemphill <hemphill at hemphills.net>
  • Date: Sun, 20 Feb 2005 00:09:40 -0500 (EST)
  • Reply-to: hemphill at alumni.caltech.edu
  • Sender: owner-wri-mathgroup at wolfram.com

On Sat, 19 Feb 2005, DrBob wrote:

> (inputs)
> old = Array[o, {4}]
> new = Array[n, {4}]
> cond = Array[1 == Random[Integer, {0, 1}] & , {4}]
> 
> (outputs)
> {o[1], o[2], o[3], o[4]}
> {n[1], n[2], n[3], n[4]}
> {True, False, True, False}
> 
> (input)
> Thread[If[cond, Evaluate[new], Evaluate[old]]]
> 
> (result)
> {n[1], o[2], n[3], o[4]}
> 
> or:
> 
> old = {a, c, x, y} = Array[o, {4}]
> new = {a, c, x, y} = old^2
> cond = Array[1 == Random[Integer, {0, 1}] &, {4}]
> 
> {o[1], o[2], o[3], o[4]}
> {o[1]^2, o[2]^2, o[3]^2, o[4]^2}
> {True, False, False, False}
> 
> {a, c, x, y} = Thread[If[cond, Evaluate@new, Evaluate@old]]
> 
> {o[1]^2, o[2], o[3], o[4]}

This isn't quite what I had in mind.  What I have is more like:

n=100
old={Array[olda,{n}], Array[oldc,{n}], Array[oldx,{n}], Array[oldy,{n}]}
new={Array[newa,{n}], Array[newc,{n}], Array[newx,{n}], Array[newy,{n}]}
cond=Table[Random[Integer]==1,{n}]

Now "a" should be assigned a list whose k'th element is picked from either 
olda[[k]] or newa[[k]] depending on cond[[k]], and similarly for c, x, and 
y.

Thanks for your help!

Scott
-- 
Scott Hemphill	hemphill at alumni.caltech.edu
"This isn't flying.  This is falling, with style."  -- Buzz Lightyear


  • Prev by Date: Re: Why does Inverse[M] hesitate?
  • Next by Date: Re: Why does Inverse[M] hesitate?
  • Previous by thread: Re: Elegant syntax for multiple conditional assignment?
  • Next by thread: Re: Re: Elegant syntax for multiple conditional assignment?