Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Jun 18 '12 at 12:52

This question came from our site for professional and enthusiast programmers.

    
Do you plan to import these in any other program than Mathematica itself? –  Szabolcs Jun 18 '12 at 6:55
    
@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. –  rsg Jun 18 '12 at 6:58
add comment

1 Answer 1

up vote 6 down vote accepted

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"]
share|improve this answer
1  
I'm using Export["data.mx.gz",data] which I think is a good compromise between files size and the speed of loading. –  Ajasja Jun 18 '12 at 13:44
    
@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. –  Szabolcs Jun 18 '12 at 13:54
    
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. –  Ajasja Jun 18 '12 at 14:06
    
@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? –  RSG Jun 19 '12 at 17:24
1  
@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 –  Szabolcs Jun 19 '12 at 17:55
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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