4
$\begingroup$

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

$\endgroup$
3

4 Answers 4

8
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Thank you for the detailed answer. It really worked !!! $\endgroup$
    – Moni
    Commented May 15, 2016 at 23:38
  • $\begingroup$ @Jason B. How can I use ReadList to read the file because I have a very huge data to read? $\endgroup$
    – Ogawa Chen
    Commented May 21, 2019 at 7:35
5
$\begingroup$

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
$\endgroup$
5
$\begingroup$

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
$\endgroup$
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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