Re: Elegant syntax for multiple conditional assignment?
- To: mathgroup at smc.vnet.net
- Subject: [mg54632] Re: Elegant syntax for multiple conditional assignment?
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Thu, 24 Feb 2005 03:21:38 -0500 (EST)
- Organization: The University of Western Australia
- References: <cv6q9a$65c$1@smc.vnet.net> <cv99b2$kkl$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <cv99b2$kkl$1 at smc.vnet.net>,
Scott Hemphill <hemphill at hemphills.net> wrote:
> > I've got a method that works:
> >
> > t=Transpose;
> > {a, c, x, y} = t[If[#[[1]],#[[2]],#[[3]]]& /@ t[{cond,t[new],t[old]}]];
> >
> > All the transpositions and array indexing make me wonder if there's a more
> > elegant way of expressing this.
> >
> > Any takers? Thanks in advance.
>
> Maybe I should have given a more concrete example:
>
> old = {Array[olda,{5}], Array[oldc,{5}], Array[oldx,{5}], Array[oldy,{5}]};
> new = {Array[newa,{5}], Array[newc,{5}], Array[newx,{5}], Array[newy,{5}]};
> cond = {True,False,True,False,True};
>
> I want to assign:
>
> a = {newa[1], olda[2], newa[3], olda[4], newa[5]}
>
> based on the conditions in cond, and similarly for c, x, and y.
Here are two possibilities that avoid array indexing:
{a, c, x, y} = Apply[If,Transpose[{cond,Last[#],First[#]}],1]& /@
Transpose[{old, new}]
{a, c, x, y} = Tr[#, List] & /@ Transpose[Thread[If[cond, new, old]]]
Cheers,
Paul
--
Paul Abbott Phone: +61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailto:paul at physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul