MathGroup Archive 2005

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

Search the Archive

Re: Re: Elegant syntax for multiple conditional assignment?

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

On Sat, 19 Feb 2005, Bob Hanlon wrote:

> a1={1,2,3,4,5};
> c1={6,7,8,9,10};
> x1={11,12,13,14,15};
> y1={16,17,18,19,20};
> 
> old={a1,c1,x1,y1};
> 
> a2={21,22,23,24,25};
> c2={26,27,28,29,30};
> x2={31,32,33,34,35};
> y2={36,37,38,39,40};
> 
> new={a2,c2,x2,y2};
> 
> cond={True,False,True,False,True};
> 
> t=Transpose;
> 
> t[If[#[[1]],#[[2]],#[[3]]]&/@t[{cond,t[new],t[old]}]] ==
>  
>        t[Table[If[cond[[n]],new[[All,n]],old[[All,n]]],{n,Length[cond]}]]
> 
> True
> 
> n=10000;
> 
> old=Table[Random[],{4},{n}];
> 
> new=Table[Random[],{4},{n}];
> 
> cond=(Table[Random[Integer,{0,1}],{n}]/.{1->True,0->False});
> 
> Timing[{a,c,x,y} = 
>       t[If[#[[1]],#[[2]],#[[3]]]&/@t[{cond,t[new],t[old]}]];]
> 
> {0.2 Second,Null}
> 
> Timing[{a,c,x,y} = t[Table[
>           If[cond[[n]],new[[All,n]],old[[All,n]]],{n,Length[cond]}]];]
> 
> {0.13 Second,Null}

Ah!  That's what I was after.  Thanks!  The timings aren't critical.  I
was merely looking for something that was simple and obviously correct.
I figured that if I asked the group, I would get something better than
my solution, and that I would learn something in the process.  In this
case, I was unaware of the "All" option to "Part".

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


  • Prev by Date: Re: Plotting symbolic representation of numerica function
  • Next by Date: Re: Plotting symbolic representation of numerica function
  • Previous by thread: Re: Elegant syntax for multiple conditional assignment?
  • Next by thread: Re: Elegant syntax for multiple conditional assignment?