Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3173

Re: How to Do a Replacement with Two Conditions

From Murray Eisenberg <murray@math.umass.edu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to Do a Replacement with Two Conditions
Date 2011-06-18 21:44 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <itj67o$k3s$1@smc.vnet.net> (permalink)
References <201106170407.AAA28768@smc.vnet.net>

Show all headers | View raw


I was with you until you said "to create a dummy variable". If you just 
want to replace numbers > 180 with 1 and those <= 180 with 0, then Boole 
will do the trick:

    myList = Append[RandomInteger[{0, 360}, 10], 180]
{235, 189, 23, 169, 229, 93, 14, 284, 251, 290, 180}

    Boole[ Thread[myList > 180] ]
{1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0}

The Thread is needed because > (Greater) is not Listable -- somewhat 
surprisingly to me.


On 6/17/2011 12:07 AM, Gregory Lypny wrote:
> Hello everyone,
>
> I have a list of integers and I want to replace numbers bigger than 180 with one and those less than or equal to 180 with 0 to create a dummy variable.  The ones are easy:
>
> 	myList /. x_ /; x>  180 ->  1
>
> How do I get the zeros in there?
>
> Regards,
>
> Gregory
>

-- 
Murray Eisenberg                     murray@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: How to Do a Replacement with Two Conditions Murray Eisenberg <murray@math.umass.edu> - 2011-06-18 21:44 +0000

csiph-web