On 5/31/05 at 5:00 AM, marloo3 at mail15.com wrote:
>please, what is the best way to reduce the timing of the following
>lines, the source of data is:
>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
A couple of comments. First, on my system using this code results in a recursion error message and will not produce any usable output.
Second, generally the best way to improve execution speed in code like this is to eliminate the For loop, replacing it with functional programming constructs and pattern matching.
As I understand this code, you want a list the same length as the original list having a 1 where ever the original list has a 1 and a zero everywhere else. If I have this correct, the following will do what you want and should run much faster
lst = (#1 == 1 & ) /@ dat /. {True -> 1, False -> 0}
--
To reply via email subtract one hundred and four