5
$\begingroup$

I am working on some problem which requires generating a list of random matrices. So I used the code (actually a shortened version of the code, but it does not matter)

L={};
For[n = 1, n <= 10, n++,
 R = RandomComplex[{-1 - 1 I, 1 + 1 I}, {2,2}];
ApppendTo[L,R]
]
Export["list.dat",L]

So far so good, but when I try to import them by

Import["list.dat"]

it imports the list but pus a ',' after each and every string. In particular, after each +, -, I, }, and after each ',' as well! Some variations (like importing as table and so on) are not giving anything better. I tried to export it as .mat format which gives the following error

Export::type: {{{-0.0542175+0.284324 I,-<<21>>-<<19>> I},{<<1>>}},<<9>>} cannot be exported to the MAT format. >>

I am using mathematica 6 and 7 both.

Advanced thanks for any hint/ suggestions.

$\endgroup$
2
  • $\begingroup$ Do you plan to import these in any other program than Mathematica itself? $\endgroup$
    – Szabolcs
    Jun 18, 2012 at 6:55
  • $\begingroup$ @Szabolcs no. i am at present working only on mathematica. i want to generate a huge list first, save it separately and then import the list to pick up matrices and do different works. $\endgroup$
    – rsg
    Jun 18, 2012 at 6:58

1 Answer 1

8
$\begingroup$

First, if you're a beginner, it's best to try to avoid For loops as much as possible. In most cases there are better ways to do thing. AppendTo in particular is quite slow because it reallocates the array on every append.

So use

data = Table[
 RandomComplex[{-1 - I, 1 + I}, {2, 2}],
 {10}
 ]

or even simpler:

data = RandomComplex[{-1 - I, 1 + I}, {10, 2, 2}]

As for exporting and importing, it's best to specify the format explicitly. Since you only plan to import the data to Mathematica, I suggest using a format that can store arbitrary expressions. There are four basic choices:

  • The WDX format, also used for example data

  • The MX format which is extremely quick to read/write but is neither cross-platform, nor cross architecture

  • The "Package" format which will just write the Mathematica expressions as strings

  • Using Compress and exporting as a string.

I recommend you use WDX if the data is not large and performance is not critical. For best performance, use MX, but be aware that the files will not be portable to other computers. For good performance and portability, use the Compress trick.

Export["data.wdx", data, "WDX"]

data = Import["data.wdx"]
$\endgroup$
6
  • 1
    $\begingroup$ I'm using Export["data.mx.gz",data] which I think is a good compromise between files size and the speed of loading. $\endgroup$
    – Ajasja
    Jun 18, 2012 at 13:44
  • $\begingroup$ @Ajasja The main point of Compress is not to reduce the file size here. It is to make the file portable. .mx.gz is still not portable between computers. $\endgroup$
    – Szabolcs
    Jun 18, 2012 at 13:54
  • $\begingroup$ Yes, I know, but I had 100 MB mx files laying around, and just adding ".gz" reduced the file size 3-4 times, while not noticeably hindering the speed of loading. $\endgroup$
    – Ajasja
    Jun 18, 2012 at 14:06
  • $\begingroup$ @Szabolcs thanks for the reply and it worked. i have written above a simplified version of the code. actually there are a few more things which i have put under 'For' loop. nevertheless, can you please tell me why you advice using 'For' loop judiciously? $\endgroup$
    – RSG
    Jun 19, 2012 at 17:24
  • 1
    $\begingroup$ @rsg For loops have their place; but in the vast majority of cases there are better ways to solve a problem than using a For loop. If you're a beginner, I recommend you try to avoid them as much as possible: this'll improve your code. If you need to stick to a procedural style, you can almost always use Do in place of For: Do is more readable for people used to Mathematica (For is not too nice in Mma as you might have noticed), and Do localized its variable, eliminating one potential source of error. For long procedural loops, Reap/Sow will be much faster than AppendTo $\endgroup$
    – Szabolcs
    Jun 19, 2012 at 17:55

Your Answer

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

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