Re: Using 'IF' function on 'Lists' in Mathematica 6.01
- To: mathgroup at smc.vnet.net
- Subject: [mg85044] Re: [mg85035] Using 'IF' function on 'Lists' in Mathematica 6.01
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Fri, 25 Jan 2008 05:03:36 -0500 (EST)
- Reply-to: hanlonr at cox.net
data = {0.25, 0.292893219, 0.5, 0.707106781, 0.75, 0, 0.25, 0.292893219, 0.5, 0.707106781, 0.75, 0, 0.25, 0.2928932190000002, 0.5, 0.7071067809999998, 0.75, 0, 0.25, 0.2928932190000002, 0.5, 0.7071067809999998, 0.75, 0, 0.5, 0.5857864380000004, 1, 1.4142135619999996, 1.5, 0, 0.5, 0.5857864380000004, 1, 1.4142135619999996, 1.5, 0, 0.5, 0.5857864379999995, 1, 1.4142135620000005, 1.5, 0, 0.5, 0.5857864399999997, 1, 1.4142135600000003, 1.5, 0, 0.75, 0.8786796599999995, 1.5, 2.1213203400000005, 2.25, 0, 0.75, 0.8786796599999995, 1.5, 2.1213203400000005, 2.25, 0, 0.75, 0.8786796599999995, 1.5, 2.1213203400000005, 2.25, 0, 0.75, 0.8786796599999995, 1.5, 2.1213203400000005, 2.25, 0}; Since there are no negative values use Sign r1 = Sign[data] {1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,\ 1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0} If there are negative values, use Sign[Abs[]] r2 = Sign[Abs[data]] {1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,\ 1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0} SetAttributes[testIf, Listable]; testIf[dataValue_] := If[dataValue == 0, 0, 1]; You can Map your function onto a list r3 = testIf /@ data {1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,\ 1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0} But since I made the function Listable r4 = testIf[data] {1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,\ 1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0} r1 == r2 == r3 == r4 True Bob Hanlon ---- Lea Rebanks <lrebanks at netvigator.com> wrote: > Dear All, > > I trying to Use the 'IF' function on a 'List' - 'data' below. > > I am trying to get the output as a 'List' of 1's & 0's > > In[7]:= data = {0.25, 0.292893219, 0.5, > 0.707106781, 0.75, 0, 0.25, > 0.292893219, 0.5, 0.707106781, > 0.75, 0, 0.25, > 0.2928932190000002, 0.5, > 0.7071067809999998, 0.75, 0, > 0.25, 0.2928932190000002, 0.5, > 0.7071067809999998, 0.75, 0, 0.5, > 0.5857864380000004, 1, > 1.4142135619999996, 1.5, 0, 0.5, > 0.5857864380000004, 1, > 1.4142135619999996, 1.5, 0, 0.5, > 0.5857864379999995, 1, > 1.4142135620000005, 1.5, 0, 0.5, > 0.5857864399999997, 1, > 1.4142135600000003, 1.5, 0, 0.75, > 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0, > 0.75, 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0, > 0.75, 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0, > 0.75, 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0} > > > In[8]:= Clear[testIf] > testIf[dataValue_] := > If[dataValue == 0, 0, 1] > > In[11]:= testIf[data[[11]]] > Out[11]= 1 > > In[13]:= testIf[data[[12]]] > Out[13]= 0 > > These two above values are working fine, however I want to produce a 'List'. > > In[12]:= testIf[data[[All]]] > Out[12]= If[{0.25, 0.292893219, 0.5, > 0.707106781, 0.75, 0, 0.25, > 0.292893219, 0.5, 0.707106781, > 0.75, 0, 0.25, > 0.2928932190000002, 0.5, > 0.7071067809999998, 0.75, 0, > 0.25, 0.2928932190000002, 0.5, > 0.7071067809999998, 0.75, 0, > 0.5, 0.5857864380000004, 1, > 1.4142135619999996, 1.5, 0, 0.5, > 0.5857864380000004, 1, > 1.4142135619999996, 1.5, 0, 0.5, > 0.5857864379999995, 1, > 1.4142135620000005, 1.5, 0, 0.5, > 0.5857864399999997, 1, > 1.4142135600000003, 1.5, 0, > 0.75, 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0, > 0.75, 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0, > 0.75, 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0, > 0.75, 0.8786796599999995, 1.5, > 2.1213203400000005, 2.25, 0} == > 0, 0, 1] > > Any help or advice gratefully received. > > Best Regards - Lea Rebanks... > > >