Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg54458] Re: [mg54337] Elegant syntax for multiple conditional assignment?
  • From: DrBob <drbob at bigfoot.com>
  • Date: Sun, 20 Feb 2005 00:11:27 -0500 (EST)
  • References: <Pine.LNX.4.44.0502191343310.3694-100000@pearl.local>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

It's just the same trick on another level. Like this:

Clear@update
update[cond_List][old_List, new_List] := Thread@If[cond, new, old]

n = 6;
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}]

{True, True, False, False, True, False}

update[cond] @@@ Transpose@{old, new};
TableForm[%, TableHeadings -> {{"a", "c", "x", "y"}, cond}]

TableForm[{{"", True, True, False, False, True, False},
    {"a", newa[1], newa[2], olda[3], olda[4], newa[5], olda[6]},
    {"c", newc[1], newc[2], oldc[3], oldc[4], newc[5], oldc[6]},
    {"x", newx[1], newx[2], oldx[3], oldx[4], newx[5], oldx[6]},
    {"y", newy[1], newy[2], oldy[3], oldy[4], newy[5], oldy[6]}},
   TableHeadings -> {{"a", "c", "x", "y"},
     {True, True, False, False, True, False}}]

Bobby

On Sat, 19 Feb 2005 13:51:36 -0500 (EST), Scott Hemphill <hemphill at hemphills.net> wrote:

> 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



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Leading Zeros? (question rephrased)
  • Next by Date: RE: graphing in Mathematica
  • Previous by thread: Re: Elegant syntax for multiple conditional assignment?
  • Next by thread: Re: Re: Elegant syntax for multiple conditional assignment?