# Save and call matrices with large output in Mathematica [duplicate]

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?

-

## marked as duplicate by rm -rf♦Mar 15 '13 at 4:44

Related answer about saving definitions that also extends to data: mathematica.stackexchange.com/a/2008/121 –  Mr.Wizard Apr 23 '12 at 6:39

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.

-
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]}} –  George Mills May 1 '12 at 13:46
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. –  Vince May 7 '12 at 3:27

If you enter:

A >> "savedA.m"


then you can get it back with:

B=<<savedA


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.

-
If you include a semi-colon after B=<<savedA  then the output will be suppressed. –  rcollyer Apr 22 '12 at 17:28
@rcollyer can you give small example? –  George Mills Apr 22 '12 at 19:17
@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. –  rcollyer Apr 22 '12 at 19:39
@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. –  George Mills Apr 22 '12 at 21:08
@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. –  rcollyer Apr 22 '12 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.

@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. –  rcollyer Apr 22 '12 at 17:27
@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. –  tkott Apr 23 '12 at 0:46