# How to Import and Export as a multidimensional array

I have 10000 matrices and each matrix is 3x3. So Dimensions[data] gives me {10000,3,3}. I saved this into a file with Export["mydata.dat",data,"Table"] Then when I imported it using Import["mydata.dat","Table"], it gives me dimensions as {10000,9}.

How can I export and Import this as a list of dimensions {10000,3,3}?

Thanks

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 May 13 '16 at 7:45
• – user9660 May 13 '16 at 7:45
• If you just need to read it back into mathematica on the same system use a ".mz" file. If you need to share with other apps you should explain what you actually need. – george2079 May 13 '16 at 13:56

You can export as a MATLAB .mat file if your array has less than 4 dimensions,

rand = RandomReal[1, {1000, 3, 3}];
Dimensions@rand
rand[[454, 1, 2]]
Export["random.mat", rand];
(* {1000, 3, 3} *)
(* 0.786307 *)


When you import it again, you have the same dimensions and the elements are the same

rand2 = Import["random.mat"];
Dimensions@rand2
rand2[[454, 1, 2]]
(* {1000, 3, 3} *)
(* 0.786307 *)


If you have 4 or more dimensions, you can export as an HDF file,

rand = RandomReal[1, {1000, 3, 3, 5, 5, 7, 4}];
Export["random.hdf", rand]
(* "random.hdf" *)


When you import it, it will tell you the names of the datasets stored in the file

Import["random.hdf"]
(* {"Dataset1"} *)


So when you import the data, you need to specify the dataset you are after,

rand2 = Import["random.hdf", {"Datasets", "Dataset1"}];
Dimensions@rand2
(* {1000, 3, 3, 5, 5, 7, 4} *)

rand == rand2
(* True *)


If you are exporting a lot of data, and don't need to read it with a text editor, then I would recommend always using one of these formats. The file sizes will be drastically smaller and read times faster because they are binary formats.

• Thank you for the detailed answer. It really worked !!! – Moni May 15 '16 at 23:38
• @Jason B. How can I use ReadList to read the file because I have a very huge data to read? – Chuan Chen May 21 '19 at 7:35

If you plan to use your data in MMA only you can do

a = RandomReal[1, {1000, 3, 3}];
Dimensions[a]
a >> testExport.dat

b = << testExport.dat;
Dimensions[b]
a == b


I absolutely support JasonB's suggestions, especially hdf5 seems a good format for such data when you want to be able to read with other software. But for the case where you only need to write and read with Mathematica, I think the MX format at least needs to be mentioned as well: it is by far the easiest and fastest way to store arbitrary expressions (not only higher dimension arrays):

a = RandomReal[1, {1000, 3, 3}];
Export[FileNameJoin[{$HomeDirectory, "Desktop", "tst.mx"}], a] b = Import[FileNameJoin[{$HomeDirectory, "Desktop", "tst.mx"}]];
a == b


The following worked for 3-dimensional arrays:

...
Dimensions[rcst]
(* (45,31,19) *)
...
Export["rcst.dat", rcst, "Table"]
...
rcst1 = ToExpression[Import["rcst.dat", "TSV"]];
Max[Abs[rcst1 - rcst]]
(* 0. *)


The point is that the imported array rcst1 is 2-dimensional, and each element of it is a STRING rather than a 1-dimensional LIST, as it was in rcst.

The option TSV is to eliminate the double commas.