# 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?

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:= A[w1_, w2_] := ( { {2 w1*w1, 4 w2*w1}, {3 w1*w2, 7 w1*w2} } ); In:= Export["A.wdx", A[w1, w2]]; In:= A[w1_, w2_] := Import["A.wdx"]; In:= A[z, z] Out= {{2 w1 w1, 4 w1 w2}, {3 w1 w2, 7 w1 w2}} – 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:= 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:= 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:= 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:= A == B

Out= 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