MathGroup Archive 2008

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

Search the Archive

Re: Using 'IF' function on 'Lists' in Mathematica 6.01

  • To: mathgroup at smc.vnet.net
  • Subject: [mg85049] Re: [mg85035] Using 'IF' function on 'Lists' in Mathematica 6.01
  • From: Curtis Osterhoudt <cfo at lanl.gov>
  • Date: Fri, 25 Jan 2008 05:06:10 -0500 (EST)
  • Organization: LANL
  • References: <200801240950.EAA15143@smc.vnet.net>
  • Reply-to: cfo at lanl.gov

  Hi, Lea, 

   As usual, there are many ways to do what you want. Your testIf function 
works fine, but only if it's applied to each of the list's elements 
one-by-one. 
   There are two ways to use testIf in the way you want:
            1) Map it over your "data" list:
                 
                     testIf/@data
                             or
                    Map[testIf, data]

             2) Make testIf work on lists. Do this by making one of 
the "Attributes" of testIf be "Listable": 
     
                     AppendTo[Attributes[testIf], Listable]
      
After that's done, your testIf[data] should work the way you expect.
 
                     Good luck, 
                              C.O.

On Thursday 24 January 2008 02:50:49 Lea Rebanks 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...



-- 
==========================================================
Curtis Osterhoudt
cfo at remove_this.lanl.and_this.gov
PGP Key ID: 0x4DCA2A10
Please avoid sending me Word or PowerPoint attachments
See http://www.gnu.org/philosophy/no-word-attachments.html
==========================================================


  • Prev by Date: Re: Wavelet "filter"?
  • Next by Date: Re: Re: List complement operator
  • Previous by thread: Re: Using 'IF' function on 'Lists' in Mathematica 6.01
  • Next by thread: Re: Using 'IF' function on 'Lists' in Mathematica 6.01