MathGroup Archive 2005

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

Search the Archive

Re: reducing the time of constructing a List


someone wrote


>dat = Table[Random[Integer, {0, 10}], {100000}];
>and the purpose is to construct the List lst in the following way:
>For[i = 1, i <= 100000,
>      If [dat[[i]] == 1,
>        lst = Join[lst, {1}], lst = Join[lst, {0}]]; i++]; // Timing
>

This is a list that turns every element not 1 into a zero.
Create a function to do so

    f[1]=1;
    f[x_]=0;

and then map it onto your original list

    lst = f /@ dat

cheers,
Peltio


  • Prev by Date: Re: dendograms
  • Next by Date: Re: reducing the time of constructing a List
  • Previous by thread: Re: reducing the time of constructing a List
  • Next by thread: Re: reducing the time of constructing a List