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: [mg54414] Re: Re: [mg54337] Elegant syntax for multiple conditional assignment?
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sun, 20 Feb 2005 00:08:39 -0500 (EST)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

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}


Bob Hanlon

> 
> From: Scott Hemphill <hemphill at hemphills.net>
To: mathgroup at smc.vnet.net
> Date: 2005/02/19 Sat AM 11:17:59 EST
> To: Bob Hanlon <hanlonr at cox.net>
> CC: mathgroup at smc.vnet.net
> Subject: [mg54414] Re: [mg54337] Elegant syntax for multiple conditional assignment?
> 
> On Sat, 19 Feb 2005, Bob Hanlon wrote:
> 
> > old={a1,c1,x1,y1};
> > 
> > new={a2,c2,x2,y2};
> > 
> > cond={True,False,True,False};
> > 
> > {a,c,x,y} = If[#[[1]],#[[2]],#[[3]]]&/@
> >     Transpose[{cond,new,old}]
> > 
> > {a2,c1,x2,y1}
> > 
> > {a,c,x,y} = If[#[[1]],#[[2]],#[[3]]]&/@
> >     Thread[{cond,new,old}]
> > 
> > {a2,c1,x2,y1}
> 
> This isn't what I want.  What I want is this:
> 
> 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}  (* Note: five elements *)
> 
> What I want is code which takes old, new, and cond and makes this 
> assignment:
> 
> a={21,2,23,4,25};
> c={26,7,28,9,30};
> x={31,12,33,14,35};
> y={36,17,38,19,40};
> 
> Scott
> -- 
> Scott Hemphill	hemphill at alumni.caltech.edu
> "This isn't flying.  This is falling, with style."  -- Buzz Lightyear
> 
> 


  • Prev by Date: RE: graphing in Mathematica
  • Next by Date: Re: Bug Report - Two numerical values for a same variable
  • Previous by thread: Re: Elegant syntax for multiple conditional assignment?
  • Next by thread: Re: Elegant syntax for multiple conditional assignment?