If we have the large matrix

A=RandomReal[{-10, 10}, {1000, 1000}];

and I need it for further calculation, how can I save it on hard disk and then call for further calculation? Output is very large and Mathematica can not present it, so I can not copy/paste it? Is it possible to print it and then call from hard disk?

Thank you in advance!


3 Answers 3

Export["A.wdx", A];

will save matrix A in file "A.wdx" on disk. WDX format is binary, compressed, and cross-platform.

Restart Mathematica and evaluate

A = Import["A.wdx"];

This will read "A" from disk file "A.wdx" and store it in symbol A.

Proceed with your computation.

  • $\begingroup$ yes, but look this problem, if the matirx is in function of variables, when I call after I can not change the variables In[3]:= A[w1_, w2_] := ( { {2 w1[1]*w1[3], 4 w2[5]*w1[6]}, {3 w1[1]*w2[3], 7 w1[1]*w2[3]} } ); In[4]:= Export["A.wdx", A[w1, w2]]; In[5]:= A[w1_, w2_] := Import["A.wdx"]; In[7]:= A[z, z] Out[7]= {{2 w1[1] w1[3], 4 w1[6] w2[5]}, {3 w1[1] w2[3], 7 w1[1] w2[3]}} $\endgroup$ May 1, 2012 at 13:46
  • $\begingroup$ Yes, my answer was limited and was biased towards saving simple data. Still you might get what you want with replacement rules, awkwardly. Say, With[{ia=Import@"A.wdx"}, A[w1_,w2_] := ReplaceAll[ia, {Globalw1 -> w1, Globalw2 -> w2}]; or some such thing. $\endgroup$
    – Vince
    May 7, 2012 at 3:27

If you enter:

A >> "savedA.m"  

then you can get it back with:


You will get a window, telling you, that a large output was generated. But B now is the same matrix as the A you saved.

  • 2
    $\begingroup$ If you include a semi-colon after B=<<savedA` then the output will be suppressed. $\endgroup$
    – rcollyer
    Apr 22, 2012 at 17:28
  • $\begingroup$ @rcollyer can you give small example? $\endgroup$ Apr 22, 2012 at 19:17
  • $\begingroup$ @GeorgeMills sure, but there isn't much beyond what Peter said. Try this, generate a matrix A = RandomReal[1, {5,5}], save it A >> "saved.m", and read it back in B=<<saved`; . Note, by adding the semi-colon after saved` , I suppressed its output, and you can verify that it is the same matrix by, in this case, enter B in another cell, or if they're to large, A==B returns true. $\endgroup$
    – rcollyer
    Apr 22, 2012 at 19:39
  • $\begingroup$ @rcollyer thank you for many comments. 1) If I want to use A >> "saved.m" how to save somewhere else, for example in another folder and then call from there? 2) It is not necessary to put A==B, I can call with A=<<saved`; after exit from kernel. $\endgroup$ Apr 22, 2012 at 21:08
  • $\begingroup$ @GeorgeMills 1) You can specify a directory directly in the filename, just like any other program, or specify the directory before hand using SetDirectory. 2) Yes, after you exit the kernel you can then use A=<<saved` to reload A. $\endgroup$
    – rcollyer
    Apr 22, 2012 at 21:57

Yes, this is possible: use DumpSave. Warning: not cross-architecture friendly. For your example:

In[18]:= A = RandomReal[{-10, 10}, {1000, 1000}];

(* for demonstration, save the same matrix A to the variable B for double checking *)
B = A;

(* Dump the variable definition, which includes the variable name, to the file *)
In[20]:= DumpSave[FileNameJoin[{$TemporaryDirectory, "a.mx"}], A];

(* Clear the variable A and it's contents from Mathematica's memory. 
Now, the only remnants of A are in the file "a.mx" and in the copied variable B *)
In[21]:= Clear[A]

(* Import and define the variable A again *)
Get[FileNameJoin[{$TemporaryDirectory, "a.mx"}]]

(* Check that our important definition of A matches what we defined at the beginning *)
In[23]:= A == B

Out[23]= True

You can now use A as if you never quit MMA or cleared the variable. So, you can try to calculate the inverse (Inverse[A]) or any other legal MMA calculation.

See also the following questions:

  • $\begingroup$ this is ok, but I need to save matrix and how to use it for further calculation? In this code to save matrix and then close the Mathematica and open again and just you want for example now Inverse[A] but without repeat calculation, it is already calculated and it is on hard disk, how to do that? $\endgroup$ Apr 22, 2012 at 16:00
  • $\begingroup$ @GeorgeMills if, instance, the matrix you want saved is the inverse of A, then first calculate Ainv = Inverse[A]; (don't forget the semi-colon), and the use Ainv instead of A in the above code. $\endgroup$
    – rcollyer
    Apr 22, 2012 at 17:27
  • $\begingroup$ @GeorgeMills If you use DumpSave, then when you use Get or << to import the dumped file, the same symbol is repopulated. Please check the documentation that I linked to. I have also edited my answer to make clear what I am showing. $\endgroup$
    – tkott
    Apr 23, 2012 at 0:46

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