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'm trying to export a sparse array from Mathematica to share with collaborators who primarily use Matlab.

The sparse array in question is 4 dimensional, (72 x 93 x 94 x 172) with ~4M non-zero elements. I've tried a number of possibilites but haven't found a working format. Is there a cross-program compatible format for large multidimensional (sparse)matrices?

The data is easily exported in the native Mathematica WDX and MX formats, except, as far as I can tell these can't be read by any other programs.

Based on this question/answer (support-for-compressed-sparse-column-sparse-matrix-representation), I've tried the HarwellBoeing RSA format.

test = SparseArray[RandomInteger[{1, 100}, {10^6, 4}] -> RandomInteger[10, 10^6]]
Export["text.rsa", test]

This returns: Export::rank: The sparse array SparseArray[<<1>>] to be exported in HarwellBoeing format is not a matrix of rank 2.

I've also tried the MatrixMarket MTX format:

Export["text.mtx", test]

This gives a different error:

Export::type: SparseArray[<<1>>] cannot be exported to the MatrixMarket format.

The matlab format, the most obvious choice, also doesn't work:

Export["text.mat", test]

Another new error this time:

Export::type: {<<1>>} cannot be exported to the MAT format.

However, the Matlab MAT format appears to support 3 dimensional sparse arrays, in this case we can export each 3D array making up the 4D array and it works, although when imported into matlab each file is actually 100 2D arrays, so the 3D actually support isn't implemented correctly and does not give a matlab 3D matrix.

MapThread[Export, {# <> ".mat" & /@ ToString /@ Range[100], test}]
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

If exporting the SparseArray does not work directly, you could perhaps use

sa=SparseArray[;;;]

sa["NonzeroPositions"]
sa["NonzeroValues"]

and then reconstruct the SparseArray in the other tool. I understand that this is most likely not ideal but it could work. Also, someone else might know better, I am not sure the RSA can deal with rank > 2 matrices.

share|improve this answer
add 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.