1
$\begingroup$

I was wondering if anyone know how to rewrite the following nested loop but using Reap and Sow. I need to reduce the execution time. Thank you.

list = {};
AbsoluteTiming[
 Do[
  AppendTo[list, {}];
  If[i > 10,
   AppendTo[list[[i]], {}];
   ]
  , {i, 1, 10^4}]
 ]
$\endgroup$

closed as unclear what you're asking by Oleksandr R., Kuba, Öskå, Karsten 7., Mr.Wizard Jan 26 '15 at 7:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ I have no idea what you are trying to accomplish. But you can get the same answer like this: list = ConstantArray[{{}}, 10^4]; list[[1 ;; 10]] = {}; in far less time. $\endgroup$ – bill s Jan 16 '15 at 3:57
1
$\begingroup$

Your question is not well posed because it is predicated on a misconception that Reap and Sow are the appropriate tools to do what you wish to accomplish. bill s, rightly ignoring the constraint you place on the solution, has given a method which is both fast and simple.

On my system, your code

list = {};
First @ AbsoluteTiming[
  Do[
   AppendTo[list, {}];
   If[i > 10, AppendTo[list[[i]], {}];],
   {i, 1, 10^4}]]

gives 0.711651 seconds. bill s' code

First @ AbsoluteTiming[
 list = ConstantArray[{{}}, 10^4]; list[[1 ;; 10]] = {};]

gives 0.000102 seconds. Both methods build the same list.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.