Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a script with a long loop and very long formatted outputs like the following example code:

Do[Print[Style[Table[RandomReal[], {30}, {15}] // MatrixForm, Blue]], {10000}]

In order to display all the output in the desired format and save them into *.nb or *.pdf, at least 2GB memory is needed, which exceeds the available upper limits of x86 OS.

How can I export the formatted results immediately into a *.nb or a PDF file so that the code does not demand so much computer memory?

share|improve this question
    
@m_goldberg thank you for editing it! –  LCFactorization Nov 24 '13 at 15:52
1  
As an aside, I would point out that RandomReal can produce matrices, so your example can be reduced to Do[Print[Style[RandomReal[1., {30, 15}] // MatrixForm, Blue]], {10000}]. –  m_goldberg Nov 25 '13 at 1:43
    
How do you expect to use the output? The file produced, whether .nb or .pdf, will too big, to open in Mathematica on your system. –  m_goldberg Nov 25 '13 at 1:54
    
I'm missing something. If you put 10000 large styled matrices into a PDF file, that file will be big. –  m_goldberg Nov 25 '13 at 2:34
    
Yes, the PDF file will be big; but usually less than 50M and requires very small memory to view it. –  LCFactorization Nov 25 '13 at 2:45

1 Answer 1

up vote 1 down vote accepted

I could not generate a notebook with 10000 30 x15 matrices without the Mathematica front-end eating up a good deal more then 2 GB of memory. Best I have done so far is create and save a notebook file with 2500 such matrices. On my system, this used up about 800 MB of Mathematica front-end memory. The good news is that it was done with one line of code:

CreateDocument @ Table[Style[RandomReal[1., {30, 15}] // MatrixForm, Blue], {2500}]; 

After saving the newly generated matrix notebook, I quit Mathematica, and then restarted it again by double-clicking on the matrix notebook's icon. The matrix notebook loaded quickly and only used about 4 MB of front-end memory. Using the File > Print... menu items, I was able to create a PDF file with two matrices per page (trying to put more on a page made the numbers too small for comfortable reading).

You asked in chat, how this approach could be modified to handle the case where each matrix generated has a dependency on an iteration variable n. Suppose this dependency was scalar multiplication by n. In this case, n would simply be the first element in the in the iteration control list, the second argument of Table:

CreateDocument @ Table[Style[n RandomReal[1., {30, 15}] // MatrixForm, Blue], {n, 2500}]; 

Should you adopt this approach, I recommend doing it with a freshly launched front-end session.

share|improve this answer
    
Thank you very much. I also tried to "output.pdf"~Create~CreateDocument@ Table[Style[n RandomReal[1., {30, 15}] // MatrixForm, Blue], {n, 2500}]; but Create[] function does not make any pagebreak; unless we handle binary PDF creation operation, this should be the best way to create good looking formatted PDF output. –  LCFactorization Nov 26 '13 at 0:21
1  
@LCFactorization. To get page breaks, image scaling, and other layout controls, I had to use Mathematica's notebook printing tools. On my system, OS X, the print dialog offers "Save as PDF" as an option, so after formatting the notebook for printing, I took advantage of that option. –  m_goldberg Nov 26 '13 at 5:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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