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: [mg54463] Re: [mg54337] Elegant syntax for multiple conditional assignment?
  • From: Scott Hemphill <hemphill at hemphills.net>
  • Date: Sun, 20 Feb 2005 00:11:47 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On Sat, 19 Feb 2005, DrBob wrote:

> 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}}]

Thanks a million!  I'm glad I wasn't satisfied with just one solution to 
the problem.  I'd hoped that I'd learn something new if I just asked 
somebody, and I sure have.

Two constructs that I've only been vaguely aware of are Thread and
Through.  Also, although I've been comfortable with the abbreviations "/@"
and "@@", I haven't been making use of "@" and "@@@".  Thanks for the 
examples.

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


  • Prev by Date: RE: text blocks graph axis?
  • Next by Date: Re: Elegant syntax for multiple conditional assignment?
  • Previous by thread: Re: Elegant syntax for multiple conditional assignment?
  • Next by thread: Re: Elegant syntax for multiple conditional assignment?