0
$\begingroup$

I have a Do loop then I want to Save data from this loop each x number of iterations. My Do is some like:

     Estra = {}; it= 30
     Do[initial = 0; Estra = Append[Estra, initial]; 
           Do[op = 1 + i; Estra = Append[Estra, op], it], {i, 1, 3}]

Then I want to Save each number of iterations, for example each that it = 10; it= 20; it=30. I put that:

      Estra = {}; it= 30
         Do[initial = 0; Estra = Append[Estra, initial]; 
           Do[op = 1 + i; Estra = Append[Estra, op]; If[it = 10; it= 
                   20; it=30, Save["data.dat", Estra]], it], {i, 1, 3}]

But this save me all Estra each time and I want a continue list without repetitions. I mean:

The out of this loop is:

 Estra = {0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
       2,2,2,2,2,2,2,2,2,0,3,3,3,3,3,3,3,3,3,
       3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,4,4,
        4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}

I want to Save this 0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2 and the this 0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3 and so on, and finaly have a file .dat:

  Estra = {0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
       2,2,2,2,2,2,2,2,2,0,3,3,3,3,3,3,3,3,3,
       3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,4,4,
        4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}
$\endgroup$
2
  • $\begingroup$ May you provide some code for what you are attempting. Looping constructs are rarely used and there may be a better approach to achieve your aim. Some code would help. $\endgroup$
    – Edmund
    Commented May 15, 2018 at 1:23
  • 1
    $\begingroup$ add counter inside the do loop and put it back to zero when it reach the value you want? $\endgroup$
    – Alucard
    Commented May 15, 2018 at 4:11

2 Answers 2

1
$\begingroup$

Do loop then I want to Save data from this loop each x number of iterations

One way might be

SetDirectory[NotebookDirectory[]];
fd = OpenWrite["foo.txt"];

xNumberOfIterations = 3;
Do[
  m = RandomReal[];
  If[Mod[n, xNumberOfIterations] == 0,
   Write[fd, m]
   ]
  ,
  {n, 1, 10}
  ];
Close[fd]
$\endgroup$
0
$\begingroup$

It isn't exactly clear how you want to save the data, but perhaps you could adapt something like this

n=1;
saved=Reap[
Do[y=RandomInteger[{1,10}];
  n=n+1;
  Print["Inside the loop we have ",{n,y}];
  If[n==4,Sow[{n,y}];n=1]
  ,{12}]
][[2,1]];
Print["And the saved results are ", saved]

That uses n to keep track of how often and when you want to save a snapshot, but prints every pass through the loop just so you can see what it is doing, and then when time comes to save the snapshot sends a copy using Sow which will be available using the Reap after the loop is finished. Try running that, look at what is printed and then look at the output from the final print to see that it shows your saved copies. You could change the If to have it save on any other condition that you need instead of what I used in this example.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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