Skip to main content
added 5 characters in body
Source Link
Henrik Schumacher
  • 112.9k
  • 7
  • 197
  • 340

First a side note: Test[s, 0] = 0 etc. does not define a matrix but a hash table. You can allocate memory with Test=ConstantArray[0,{5,5}] and access the data with double brackets [[ ]] (see also the documentation of Part).

Towards your question. Your algorithm as an iterative structure, so I suggest to use Nest instead of For (really, everyting is better than using For! There are various post on this site that will explain to you why.)

This is the function for each iteration. It needs the iteration counter r and the current state (the current matrix row). Write writes to a file specified by the global variable stream that is yet to be defined.

step[{r_, state_}] := With[{newstate = state + Range[Length[state]] + r}, Write[stream, newstate]; {r + 1, newstate} ] 

This opens the stream, processedprocesses step in a Nested way and finally closes the stream.

stream = OpenWrite["a.dat"]; Nest[step, {1, ConstantArray[0, 5]}, 5] Close[stream] 

This is a very crude way to write to file and it produces a file that Mathematica cannot importbe imported directly with Import. But you can import line by line with

stream = OpenRead["a.dat"]; Read[stream] Close[stream] 

Of course, you have to execute Read[stream] once for each line you want to get.

First a side note: Test[s, 0] = 0 etc. does not define a matrix but a hash table. You can allocate memory with Test=ConstantArray[0,{5,5}] and access the data with double brackets [[ ]] (see also the documentation of Part).

Towards your question. Your algorithm as an iterative structure, so I suggest to use Nest instead of For (really, everyting is better than using For! There are various post on this site that will explain to you why.)

This is the function for each iteration. It needs the iteration counter r and the current state (the current matrix row). Write writes to a file specified by the global variable stream that is yet to be defined.

step[{r_, state_}] := With[{newstate = state + Range[Length[state]] + r}, Write[stream, newstate]; {r + 1, newstate} ] 

This opens the stream, processed step in a Nested way and finally closes the stream.

stream = OpenWrite["a.dat"]; Nest[step, {1, ConstantArray[0, 5]}, 5] Close[stream] 

This is a very crude way to write to file and it produces a file that Mathematica cannot import directly with Import. But you can import line by line with

stream = OpenRead["a.dat"]; Read[stream] Close[stream] 

Of course, you have to execute Read[stream] once for each line you want to get.

First a side note: Test[s, 0] = 0 etc. does not define a matrix but a hash table. You can allocate memory with Test=ConstantArray[0,{5,5}] and access the data with double brackets [[ ]] (see also the documentation of Part).

Towards your question. Your algorithm as an iterative structure, so I suggest to use Nest instead of For (really, everyting is better than using For! There are various post on this site that will explain to you why.)

This is the function for each iteration. It needs the iteration counter r and the current state (the current matrix row). Write writes to a file specified by the global variable stream that is yet to be defined.

step[{r_, state_}] := With[{newstate = state + Range[Length[state]] + r}, Write[stream, newstate]; {r + 1, newstate} ] 

This opens the stream, processes step in a Nested way and finally closes the stream.

stream = OpenWrite["a.dat"]; Nest[step, {1, ConstantArray[0, 5]}, 5] Close[stream] 

This is a very crude way to write to file and it produces a file that Mathematica cannot be imported directly with Import. But you can import line by line with

stream = OpenRead["a.dat"]; Read[stream] Close[stream] 

Of course, you have to execute Read[stream] once for each line you want to get.

Source Link
Henrik Schumacher
  • 112.9k
  • 7
  • 197
  • 340

First a side note: Test[s, 0] = 0 etc. does not define a matrix but a hash table. You can allocate memory with Test=ConstantArray[0,{5,5}] and access the data with double brackets [[ ]] (see also the documentation of Part).

Towards your question. Your algorithm as an iterative structure, so I suggest to use Nest instead of For (really, everyting is better than using For! There are various post on this site that will explain to you why.)

This is the function for each iteration. It needs the iteration counter r and the current state (the current matrix row). Write writes to a file specified by the global variable stream that is yet to be defined.

step[{r_, state_}] := With[{newstate = state + Range[Length[state]] + r}, Write[stream, newstate]; {r + 1, newstate} ] 

This opens the stream, processed step in a Nested way and finally closes the stream.

stream = OpenWrite["a.dat"]; Nest[step, {1, ConstantArray[0, 5]}, 5] Close[stream] 

This is a very crude way to write to file and it produces a file that Mathematica cannot import directly with Import. But you can import line by line with

stream = OpenRead["a.dat"]; Read[stream] Close[stream] 

Of course, you have to execute Read[stream] once for each line you want to get.